PHP multidimensional array is an array of arrays.
As the name suggests, every element in this array can be an array and they can also hold other sub-arrays within.
A multidimensional array is an array containing one or more arrays. It allows you to store tabular data in an array.
Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions.
PHP Multidimensional Array Example
Let’s see a simple example of PHP multidimensional array to display following tabular data. In this example, we are displaying 5 rows and 3 columns.
Id | Name | Salary |
---|---|---|
1 | Anil | 400000 |
2 | Smith | 600000 |
3 | Peter | 200000 |
4 | John | 500000 |
5 | Rahul | 300000 |
Example
<?php
$emp = array
(
array(1,"Anil",400000),
array(2,"Smith",600000),
array(3,"Peter",200000),
array(4,"John",500000),
array(5,"Rahul",300000)
);
for ($row = 0; $row < 5; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $emp[$row][$col]." ";
}
echo "<br/>";
}
?>