PHP-Multidimensional arrays
A multidimensional array refers to an array that contains one or more arrays.
notes: the dimensions of the array indicate the number of indexes you need to select an element.
- For a two-dimensional array, you need two indexes to select the element
- For three-dimensional arrays, you need three indexes to select the elements
<?php
/*Date: 2014-09-14
* Gechong
* Function: Small example of two-dimensional array
* */
$cars=Array
(
Array("Volvo", 22,18),
Array("BMW", 15,13),
Array("Saab", 5,2),
Array("Land Rover", 17,15)
);
Echo$cars[0] [0]. ": Inventory:".$cars[0] [1]. ", Sales:".$cars[0] [2]. ". <br> ";
Echo$cars[1] [0]. ": Inventory:".$cars[1] [1]. ", Sales:".$cars[1] [2]. ". <br> ";
Echo$cars[2] [0]. ": Inventory:".$cars[2] [1]. ", Sales:".$cars[2] [2]. ". <br> ";
Echo$cars[3] [0]. ": Inventory:".$cars[3] [1]. ", Sales:".$cars[3] [2]. ". <br> ";
?>
<?php
/*Date: 2014-09-14
* Gechong
* Function: Small example of two-dimensional array
* */
$cars=Array
(
Array("Volvo", 22,18),
Array("BMW", 15,13),
Array("Saab", 5,2),
Array("Land Rover", 17,15)
);
Echo"<table border =3>";
for($row= 0;$row< 4;$row++) {
Echo"<tr>";
for($col= 0;$col< 3;$col++) {
Echo"<td>".$cars[$row][$col]." <td> ";
}
Echo"<tr>";
}
Echo"</table>";
?>
PHP Multidimensional Arrays