A multidimensional array is an array of arrays. In PHP, multidimensional arrays can have more than one level of depth, which allows you to store more complex data structures like tables, records, or grids.
The syntax for creating a multidimensional array is as follows:
// Creating a basic multidimensional array
$array = array(
array("value1", "value2"),
array("value3", "value4")
);
Here's a simple example of a multidimensional array:
// Defining a multidimensional array of students
<?php
$students = array(
array("Peter", 35, "Math"),
array("John", 30, "Science"),
array("Doe", 25, "History")
);
echo $students[0][0];
?>
Peter
The first index selects the first student, and the second index selects the student's name.
Click on any cell below to see how to access that specific data using PHP indices:
| Index [0] (Name) |
Index [1] (Age) |
Index [2] (Subject) |
|
|---|---|---|---|
| Row [0] | Peter | 35 | Math |
| Row [1] | John | 30 | Science |
| Row [2] | Doe | 25 | History |