WS WhereIsStuff
Tutorial Guides
Log In
Sign Up

Advertisement

Google Ad Slot: content-top

PHP Multidimensional Arrays


A multidimensional array in PHP is an array containing one or more arrays. Multidimensional arrays allow you to create complex data structures, such as matrices, tables, or any nested structure. PHP supports both indexed and associative arrays as sub-arrays in a multidimensional array.

Note

The dimension of an array indicates the number of indices required to access a specific element in that array.

  • You need 2 indices to access an element of two-dimensional array
  • You need 3 indices to access an element of three-dimensional array

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 3 rows and 2 columns.

Name

Age

John

30

Alice

25

Bob

35

To get access to the elements of the above table we must point to the two indices (row and column):

Example
<?php
$multiArray = [
["name" => "John", "age" => 30],
["name" => "Alice", "age" => 25],
["name" => "Bob", "age" => 35]
];

// Accessing elements
echo $multiArray[1]["name"]; // Output: Alice
echo $multiArray[0]["age"]; // Output: 30
?>
Try it yourself