← Back to Chapters

PHP Multidimensional Array

? PHP Multidimensional Array

? Quick Overview

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.

? Key Concepts

  • Each element of a multidimensional array is itself another array
  • Used to represent structured data such as tables or records
  • Accessed using multiple index values

? Syntax / Theory

The syntax for creating a multidimensional array is as follows:

? View Code Example
// Creating a basic multidimensional array
$array = array(
array("value1", "value2"),
array("value3", "value4")
);

? Code Example

Here's a simple example of a multidimensional array:

? View Code Example
// 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];
?>

? Live Output / Explanation

Output

Peter

The first index selects the first student, and the second index selects the student's name.

? Interactive Visualization

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
Click a cell above to see the PHP selector!

? Use Cases

  • Student management systems
  • Database-like table structures
  • Product inventories
  • Grid-based data representation

✅ Tips & Best Practices

  • Use multidimensional arrays to represent structured datasets
  • Maintain consistent sub-array formats
  • Use loops to efficiently process nested data

? Try It Yourself

  • Create a multidimensional array of students and display their details using loops
  • Build a store inventory with product name, price, and stock count
  • Add a new field like phone number to an existing student record