It’s Festive Season! Enjoy 30% Off

Use code FESTIVE30 at checkout

Code has been added to clipboard!

PHP Multidimensional Array Explained: When and How to Use It

Reading time 3 min
Published Aug 8, 2017
Updated Oct 2, 2019

PHP arrays are considered as one of the most complex PHP data types. They're divided into three categories among themselves: indexed, associative and multidimensional.

PHP multidimensional array consists of different arrays that are organized in the same way as elements in regular arrays. If you're a beginner, we suggest that you start from using the PHP 2 dimensional array as it's easier to understand.

When you create a multidimensional array, each of the assigned elements is an array. A regular array consists of values.

PHP multidimensional array organizes your code, but its application might complicate the process of retrieving specific data. Use them with caution.

PHP Multidimensional Array: Main Tips

  • Multidimensional array is also know as PHP array of arrays.
  • Allows the developer to store values with multiple keys.
  • PHP multidimensional arrays can have as many dimensions as you like. However, the more you create, the harder they are to manage.
  • The amount of dimensions of the PHP array specifies how many indices (#) you'll need to select an item.
  • You can also make PHP sort multidimensional array by using special functions.

Multidimensional Arrays Explained

Multidimensional PHP arrays can have any number of dimensions, starting from two.

Two-dimensional PHP arrays contain arrays in which variables are stored.

Three-dimensional PHP arrays hold arrays, that hold other arrays, that then contain variables.

Four-dimensional PHP arrays contain arrays that hold other arrays, that hold arrays, that have variables in them.

The more dimensions a specific PHP multidimensional array has, the deeper are the variables buried. Getting to them becomes more and more of a hassle.

Example:

Name In stock Sold
Volkswagen 23 19
BMW 14 13
Honda 6 3
Audi 18 14

The example below shows how we can store this in a PHP multidimensional array:

Example
<?php
  for ($rows = 0; $rows < 4; $rows++) {
    echo "<p><b>Row $rows</b></p>";
    echo "<ul>";
    for ($cols = 0; $cols < 3; $cols++) {
      echo "<li>".$cars[$rows][$cols]."</li>";
    }
    echo "</ul>";
  }
?>

You can see the information from the table represented in a two-dimensional array in PHP script. The dimensions can be interpreted as rows and columns.

The PHP array of arrays we are handling has two dimensions. If we wish to access our variables, we will need to use two indices:

Example
<?php
  echo $cars[0][0].": Available: ".$cars[0][1].", sold:".$cars[0][2].".<br>";
  echo $cars[1][0].": Available: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
  echo $cars[2][0].": Available: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
  echo $cars[3][0].": Available: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>

Following the same principle, we can also use the for loop. If you need a reminder on this subject, feel free to read this tutorial. However, we must still remember the number of indices to point to:

Example
<?php
for ($rows = 0; $rows < 4; $rows++) {
  echo "<p><b>Row $rows</b></p>";
  echo "<ul>";
  for ($cols = 0; $cols < 3; $cols++) {
    echo "<li>".$cars[$rows][$cols]."</li>";
  }
  echo "</ul>";
}
?>

PHP Multidimensional Array: Summary

  • An array than contains more than one array is called an array of arrays, or a multidimensional array in PHP.
  • Use it to hold values that have more than one key.
  • Multidimensional arrays can have multiple dimensions.
  • There are special functions used to PHP sort multidimensional arrays.
  • To select an item, you need to use as many indices (#) as there are dimensions.