A multidimensional array refers to an array that contains one or more arrays.
PHP can understand multi-dimensional arrays of two or three, four, or five levels or more. However, arrays that are more than three levels deep are difficult for most people to manage.
Note: The dimensions of an 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-two-D arrays
A two-d array is an array of arrays (three-dimensional arrays are arrays of arrays).
First, let's take a look at the following table:
We can store the data in the table in a two-D array, like this:
$cars =Array (Array ("Volvo", A, -), Array ("BMW", the, -), Array ("Saab",5,2), Array ("Land Rover", -, the) );
Now this two-dimensional array contains four arrays, and it has two indexes (subscript): Rows and columns.
To access the elements in the $cars array, we must use two indexes (rows and columns):
Example
<?Phpecho $cars [0][0].": Stock:". $cars [0][1].", Sales:". $cars [0][2].".<br>"; Echo $cars [1][0].": Stock:". $cars [1][1].", Sales:". $cars [1][2].".<br>"; Echo $cars [2][0].": Stock:". $cars [2][1].", Sales:". $cars [2][2].".<br>"; Echo $cars [3][0].": Stock:". $cars [3][1].", Sales:". $cars [3][2].".<br>";?>
We can also use another for loop in the for loop to get the elements in the $cars array (we still need to use two indexes):
Example
<?PHP for($row =0; $row <4; $row + +) {echo"<p><b>row number $row </b></p>"; Echo"<ul>"; for($col =0; $col <3; $col + +) {echo"<li>". $cars [$row] [$col]."</li>"; } Echo"</ul>";}?>
PHP-Multidimensional arrays