Multi-dimensional arrays

Multi-dimensional arrays

Arrays are 1-dimensional objects.

A multi-dimensional array can be considered a list of arrays.
Multi-dimensional array structures are created by using arrays of arrays (2-d structures), arrays of arrays of arrays (3-d structures) etc.

Example of a multidimensional array matrix:

Example of a multidimensional array matrix:

0,0 1,0 2,0
0,1 1,1 2,1
0,2 1,2 2,2
0,3 1,3 2,3

Locating an element in the matrix:

The values within the multi-dimensional array is accessed using two indices:
array[row#][col#]= data
array[0][0]= "data in row0, col0";
array[0][1]= "data in row0, col1";

grid[1][1] is 5, grid[2][1] is 6

A multi-dimensional array in PHP is created using multiple calls to array:

PHP Code

<?php
 
// A 2x2 matrix with the values 1, 2 in the first row, and 3, 4 in the second.
$matrix = array(array(1, 2), array(3, 4));
 
// 'Matrix value at position 1, 1 is: 4'
print("Matrix value at position 1, 1 is: ".$matrix[1][1]."<br/>");
 
?>

0,0
1
1,02
0,1
3
1,1
4

The output of $matrix[1][1] is: 4

Example using associative arrays:

PHP Code

<?php
$array = array(
"names" => array('person1' => 'Jane',
'person2' => 'John',
'person3' => 'Fred',
'person4' => 'Anne'),
"places" => array('place1' => 'America',
'place2' => 'England',
'place3' => 'Australia',
'place4' => 'South Africa'),
"jobs" => array('job1' => 'accountant',
'job2' => 'engineer',
'job3' => 'web designer',
'job4' => 'secretary')
);
 
echo $array['names']['person1']."<br/>"; // returns Jane.
echo $array['names']['person3']."<br/>"; // returns Fred.
echo $array['places']['place1']."<br/>"; // returns America.
echo $array['places']['place3']."<br/>"; // returns Australia.
echo $array['jobs']['job1']."<br/>"; // returns accountant.
echo $array['jobs']['job3']."<br/>"; // returns web designer.
 
echo key($array['names'])."<br/>"; // returns person1.
 
?>

A good way of seeing the contents of an array, nicely formated, is to use the print_r function with the

 tag.

PHP Code
<?php
$myArray = array('test','test2','test3');
print "<pre>";
print_r($myArray);
print "</pre>";
?>
Output: Array ( [0] => test [1] => test2 [2] => test3 ) You may use complex syntax (curly braces around variable names) when printing multidimensional arrays that contain associative arrays (string indexed arrays). The array values could also be concatenated in your echo/print statement. See PHP: Strings - Manual for more information. PHP Code
<?php
 
$food[1] = array("peach","apple","orange");
$fruit = $food[1][1];
echo "<p>Fruit: $fruit";
 
//trying to print regularly
echo "<p>1:$food[1][0] 2:$food[1][1] 3:$food[1][2]<br/>";
 
//printing by concatenation
print $food[1][0].' '.$food[1][1].' '.$food[1][2].'<br/>';
 
//printing using complex syntax
print "{$food[1][0]} {$food[1][1]} {$food[1][2]}<br/>";
 
?>
Output: Fruit: apple 1:Array[0] 2:Array[1] 3:Array[2] peach apple orange peach apple orange The foreach control structure should be used to access the contents of an array easily. You can perform operations on every element in an array or only on some elements. PHP Code
<?php
$books = array(
1 => array('PHP by Example by Toby Butzon','20.99'),
2 => array('Php Fast & Easy Web Development by Julie C. Meloni','17.49'),
3 => array('MySQL/PHP Database Applications by Jay Greenspan, Brad Bulger','27.99'),
4 => array('MySQL and mSQL by Randy Jay Yarger, George Reese, Tim King','24.46'),
5 => array('Beginning PHP4 by Chris Lea, et al','27.99'),
6 => array('Professional PHP Programming by Sascha Schumann, et al','34.99')
);
 
echo "<p>Title - ".$books[1][0]." price: ".$books[1][1];
echo "<p>Title - ".$books[2][0]." price: ".$books[2][1];
 
// printing the array key and value of an array in a multi-dimensional array
foreach ($books as $book) {
foreach ($book as $key => $value){
echo "Key: $key <br/> Value: $value <br/>\n";
}
}
 
// using values in a different array to control printing of an array in a multi-dimensional array
$list = array(1, 2, 5, 6);
 
foreach ($list as $j) {
$title = $books[$j][0];
$price = $books[$j][1];
$totalqty += $price;
echo "$title - $price <br/>\n";
}
echo "total: $totalqty <br/>\n";
?>