Using Multidimensional Arrays in PHP

php

Using Multidimensional Arrays in PHP

Array elements in PHP can hold values of any type, such as numbers, strings and objects. They can also hold other arrays, which means you can create multidimensional, or nested, arrays.

In this tutorial you learn how to create multidimensional arrays, how to access elements in a multidimensional array, and how to loop through multidimensional arrays.

How to create a multidimensional array

You create a multidimensional array using the array() construct, much like creating a regular array. The difference is that each element in the array you create is itself an array.

For example:

$myArray = array(
  array( value1, value2, value3 ),
  array( value4, value5, value6 ),
  array( value7, value8, value9 )
);

The above example creates a 2-dimensional array. The top-level array contains 3 elements. Each element is itself an array containing 3 values.

You can also use associative arrays in multidimensional arrays. The following example creates an indexed array containing 3 associative arrays:

$movies = array(
  array(
    "title" => "Rear Window",
    "director" => "Alfred Hitchcock",
    "year" => 1954
  ),
  array(
    "title" => "Full Metal Jacket",
    "director" => "Stanley Kubrick",
    "year" => 1987
  ),
  array(
    "title" => "Mean Streets",
    "director" => "Martin Scorsese",
    "year" => 1973
  )
);

You can nest arrays as many levels deep as you like (although it’s rare to go higher than 3 levels). Here’s a 3-dimensional array:

$myArray = array(
  array(
    array( value1, value2 ),
    array( value3, value4 )
  ),
  array(
    array( value5, value6 ),
    array( value7, value8 )
  )
);

Accessing elements in a multidimensional array

To access multidimensional array elements, you can use the same square bracket syntax that you use with regular arrays. If you want to access the second-level array elements in a 2-dimensional array, just use 2 sets of square brackets — for example:

$myArray = array(
  array( "one", "two", "three" ),
  array( "four", "five", "six" )
);

// Displays "six"
echo $myArray[1][2];
?>

Here are some examples that access various elements in the $movies multidimensional array created earlier:

echo "The title of the first movie:<br />";
echo $movies[0]["title"] . "<br /><br />";

echo "The director of the third movie:<br />";
echo $movies[2]["director"] . "<br /><br />";

echo "The nested array contained in the first element:<br />";
print_r( $movies[0] );
echo "<br /><br />";

The above code produces the following output:

The title of the first movie:
Rear Window

The director of the third movie:
Martin Scorsese

The nested array contained in the first element:
Array ( [title] => Rear Window [director] => Alfred Hitchcock [year] => 1954 )

The last example uses $movies[0] to access the entire nested array contained in the first element of the top-level array, then uses print_r() to display the array’s contents. (Find out more about print_r() in Working With Array Elements in PHP.)

Looping through multidimensional arrays

Just as with regular, single-dimensional arrays, you can use foreach to loop through multidimensional arrays. To do this, you need to create nested foreach loops — that is, one loop inside another:

  1. The outer loop reads each element in the top-level array.
  2. For each of those top-level elements, the inner loop moves through the array contained in the top-level element, and so on.

Here’s an example that creates a 2-dimensional array of movie information, then loops through the array, displaying the information in the page:

$movies = array(
  array(
    "title" => "Rear Window",
    "director" => "Alfred Hitchcock",
    "year" => 1954
  ),
  array(
    "title" => "Full Metal Jacket",
    "director" => "Stanley Kubrick",
    "year" => 1987
  ),
  array(
    "title" => "Mean Streets",
    "director" => "Martin Scorsese",
    "year" => 1973
  )
);

foreach ( $movies as $movie ) {

  echo '<dl style="margin-bottom: 1em;">';

  foreach ( $movie as $key => $value ) {
    echo "<dt>$key</dt><dd>$value</dd>";
  }

  echo '</dl>';
}

The above script displays the following:

title
    Rear Window
director
    Alfred Hitchcock
year
    1954

title
    Full Metal Jacket
director
    Stanley Kubrick
year
    1987

title
    Mean Streets
director
    Martin Scorsese
year
    1973

Summary

In this tutorial you’ve seen how to create, manipulate, and loop through multidimensional arrays in PHP. Multidimensional arrays are useful for all sorts of things, such as holding multiple database records and storing data to display in tables. Now that you know how to use them, you can add a new level of sophistication to your PHP scripts.

Happy coding!

Leave a Reply

Your email address will not be published.