A multidimensional array contains arrays as values. Access deeper values by chaining keys or indexes, and use nested foreach loops when you need to traverse every level.
$students = [
['name' => 'Ana', 'mark' => 81],
['name' => 'Ben', 'mark' => 76]
];
echo $students[0]['name'];For record-like arrays, descriptive string keys make nested data easier to read and maintain.

$a=array(array("path"=>"php_tutorial","section"=>"php"),
array("path"=>"sql_tutorial","section"=>"sql"),
array("path"=>"javascript_tutorial","section"=>"js"),
array("path"=>"html_tutorial","section"=>"html"),
);
Now to display the elements of this array we can use like this.
$max=sizeof($a);
for($i=0; $i<$max; $i++) {
foreach ($a[$i] as $key => $val) {
echo "$key -> $val <br>";
} // inner array while loop
} // outer array for loop
The output is here
path -> php_tutorial
section -> php
path -> sql_tutorial
section -> sql
path -> javascript_tutorial
section -> js
path -> html_tutorial
section -> html
To display individual element
echo $a[2]['path'];
Output is here
javascript_tutorial
$a=array(array("product"=>"apple","quantity"=>2),
array("product"=>"Orange","quantity"=>4),
array("product"=>"Banana","quantity"=>5),
array("product"=>"Mango","quantity"=>7),
);
$b=array("product"=>"Lemon","quantity"=>9);
array_push($a,$b);
//$a[]=$b;
$max=sizeof($a);
for($i=0; $i<$max; $i++) {
foreach ($a[$i] as $key => $val) {
echo "$key -> $val ";
} // inner array while loop
echo "<br>";
} // outer array for loop
$data=array("id"=>$row['id'],"name"=>$row['name']);
Note that each student record is an array and it is added to our main array. There are two columns we used and you can extend it to include more columns.
<?Php
require "config.php";// database connection
$sql="SELECT * FROM student";
$my_array=array();
foreach ($dbo->query($sql) as $row) {
//echo "$row[id] , $row[name],$row[class],$row[mark]<br>";
$data=array("id"=>$row['id'],"name"=>$row['name']);
$my_array[]=$data;
}
echo $my_array[5]['name']; // displaying single record
echo "<hr>";
$max=sizeof($my_array);
for($i=0; $i<$max; $i++) {
// displaying all the elements
foreach ($my_array[$i] as $key => $val) {
echo "$key -> $val <br>";
} // inner array while loop
} // outer array for loop
?>
echo $my_array[5]['name']; // displaying single record
You can read more on how to connect to databaseAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.