PHP Arrays | PHP - Hypertext Preprocessor
Standards Based Development
// http://www.howtoforge.com/php_arrays
// create array
$Top3Sites = array ("fastcreators.com","howtoforge.com","scriptsbible.com");
print_r($Top3Sites);
Array 02
- Name: Lisa, Age: 28
- Name: Jack, Age: 16
- Name: Ryan, Age: 35
- Name: Rachel, Age: 46
- Name: Grace, Age: 34
// http://www.howtoforge.com/php_arrays
$students = array(); // define array students
//assigning values
$students[0] = "david";
$students[1] = "kevin";
$students[2] = "julie";
$students[3] = "nayyar";
/*
now we will use the foreach loop to display all the students names i.e the array values in one go
*/
foreach ( $students as $std_name ) {
echo $std_name . "\n";
}
Multidimensional Arrays
Two-Dimensional Arrays
Manual access to each element
rose costs 1.25 and you get 15daisy costs 0.75 and you get 25
orchid costs 1.15 and you get 7
Using loops to display array elements
- The row number 0
- rose
- 1.25
- 15
- The row number 1
- daisy
- 0.75
- 25
- The row number 2
- orchid
- 1.15
- 7
Associative Two-Dimensional Arrays
Perhaps, instead of the column numbers you prefer to create their names. For this purpose, you can use associative arrays. The following code will store the same set of flowers using column names:
It is easier to work with this array, in case you need to get a single value out of it. Necessary data can be easily found, if you turn to a proper cell using meaningful row and column names that bear logical content. However, we are loosing the possibility to use simple for loop to view all columns consecutively. You can view outer numerically indexed $shop array using the for loop. Each row of the $shop array is an associative array. Hence, inside the for loop you need for each loop. Also you can get each element from associative array manualy:
Manual access to each element from associative array
rose costs 1.25 and you get 15daisy costs 0.75 and you get 25
orchid costs 1.15 and you get 7
Using foreach loop to display elements
- The row number 0
- rose
- 1.25
- 15
- The row number 1
- daisy
- 0.75
- 25
- The row number 2
- orchid
- 1.15
- 7
Three-Dimensional Arrays
- The layer number 0
- The row number 0
- rose
- 1.25
- 15
- The row number 1
- daisy
- 0.75
- 25
- The row number 2
- orchid
- 1.15
- 7
- The row number 0
- The layer number 1
- The row number 0
- rose
- 1.25
- 15
- The row number 1
- daisy
- 0.75
- 25
- The row number 2
- orchid
- 1.15
- 7
- The row number 0
- The layer number 2
- The row number 0
- rose
- 1.25
- 15
- The row number 1
- daisy
- 0.75
- 25
- The row number 2
- orchid
- 1.15
- 7
- The row number 0
Arrays with count()
Indexed Arrays with count()
The following example utilizes count() by passing in an indexed array whose elements you need counted.
Total number of elements in the indexed array: 4
<?php
$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
echo "<p>Number of elements in the array: <b>";
// Displays "4"
echo count( $directors );
echo "</b></p>"; ?>
Associative Arrays with count()
The following example utilizes count() by passing in an associative array whose elements you need counted.
Total number of elements in the associative array: 4