Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And each element in
the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using
multiple index.
Example
In this example we create a two dimensional array to store marks of three students in three subjects −
This example is an associative array, you can create numeric array in the same fashion.
<html>
<body>
<?php
$marks = array(
"mohammad" => array (
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"qadir" => array (
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
"zara" => array (
"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
/* Accessing multi-dimensional array values */
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for qadir in maths : ";
echo $marks['qadir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />";
?>
</body>
</html>
This will produce the following result −
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
Extracting Multiple Values
To copy all of an array’s values into variables, use the list() construct:
list ($variable, ...) = $array;
The array’s values are copied into the listed variables in the array’s internal order. By
default that’s the order in which they were inserted, but the sort functions described
later let you change that. Here’s an example:
$person = array("Fred", 35, "Betty");
list($name, $age, $wife) = $person;
// $name is "Fred", $age is 35, $wife is "Betty"
If you have more values in the array than in the list(), the extra values are ignored:
$person = array("Fred", 35, "Betty");
list($name, $age) = $person; // $name is "Fred", $age is 35
If you have more values in the list() than in the array, the extra values are set to NULL:
$values = array("hello", "world");
list($a, $b, $c) = $values; // $a is "hello", $b is "world", $c is NULL
Two or more consecutive commas in the list() skip values in the array:
$values = range('a', 'e'); // use range to populate the array
list($m, , $n, , $o) = $values; // $m is "a", $n is "c", $o is "e"
Converting Between Arrays and Variables
PHP provides two functions, extract() and compact(), that convert between arrays and variables. The
names of the variables correspond to keys in the array, and the values of the variables become the
values in the array. For instance, this array
$person = array('name' => "Fred", 'age' => 35, 'wife' => "Betty");
can be converted to, or built from, these variables:
$name = "Fred";
$age = 35;
$wife = "Betty";
Creating Variables from an Array
The extract() function automatically creates local variables from an array. The indices of the array
elements become the variable names:
extract($person); // $name, $age, and $wife are now set
$name = "Fred";
$age = 35;
$wife = "Betty";
Creating an Array from Variables
The compact() function is the reverse of extract(); you pass it the variable names to compact either as
separate parameters or in an array. The compact() function creates an associative array whose keys
are the variable names and whose values are the variable’s values. Any names in the array that do not
correspond to actual variables are skipped. Here’s an example of compact() in action:
$color = "indigo";
$shape = "curvy";
$floppy = "none";
$a = compact("color", "shape", "floppy");
// or
$names = array("color", "shape", "floppy");
$a = compact($names);
Traversing Arrays
There are several ways to traverse arrays in PHP, and the one you choose will depend on your data and
the task you’re performing.
The foreach Construct - The most common way to loop over elements of an array is to use the foreach
construct:
$addresses = array("spam@cyberpromo.net", "abuse@example.com");
foreach ($addresses as $value) {
echo "Processing {$value}\n";
}
Processing spam@cyberpromo.net
Processing abuse@example.com
PHP executes the body of the loop (the echo statement) once for each element of $addresses in turn,
with $value set to the current element. Elements are processed by their internal order.
An alternative form of foreach gives you access to the current key:
$person = array('name' => "Fred", 'age' => 35, 'wife' => "Wilma");
foreach ($person as $key => $value) {
echo "Fred's {$key} is {$value}\n";
}
Fred's name is Fred
Fred's age is 35
Fred's wife is Wilma
In this case, the key for each element is placed in $key and the corresponding value is
placed in $value