PHP Array
Create an Array in PHP +
In PHP, the array() function is used to create an array:
array();
In PHP, there are three types of arrays:
- - Arrays with numeric index
- - Arrays with named keys
- - Arrays containing one or more arrays
$cars=array("Volvo","BMW","Toyota"); or the index can be assigned manually: $cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota"; ----------------------------------------- $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; The output will be: I like Volvo, BMW and Toyota. --------------------------------------------- Loop through an indexed array $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars);
for($x=0;$x<$arrlength;$x++) { echo $cars[$x]; } The output will be: Volvo BMW Toyota
PHP Associative Arrays +
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); or: $age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43";
The named keys can then be used in a script:
Example:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; The output will be: Peter is 35 years old.
Loop Through an Associative Array:
To loop through and print all the values of an associative array, you could use a foreach loop, like this:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } The output will be: Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43
PHP - Multidimensional Arrays +
A multidimensional array is an array containing one or more arrays.
PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
Note: The dimension of an array indicates the number of indices you need to select an element.
For a two-dimensional array you need two indices to select an element
For a three-dimensional array you need three indices to select an element
$cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column. To get access to the elements of the $cars array we must point to the two indices (row and column): echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2]; echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2]; echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2]; echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2]; The output will be: Volvo: In stock: 22, sold: 18. BMW: In stock: 15, sold: 13. Saab: In stock: 5, sold: 2. Land Rover: In stock: 17, sold: 15. ----------------------------------------------- Three Dimensional Array: ARRAY0 { key0.0 => "value0.0", key0.1 => "value0.1", key0.2 => ARRAY1 { key1.0 => "value1.0", key1.1 => ARRAY2 { key2.0 => "value2.0", key2.1 => "value2.1", }, key1.2 => "value1.2", }, key0.3 => "value0.3", };
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement
The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).
The second form will additionally assign the current element's key to the $key variable on each iteration.