Multidimensional arrays

One key advantage to arrays is that they can contain other arrays. Currently our arrays just hold standard, non-array variables, which makes them one-dimensional. However, by switching to a two-dimensional array - each element holds an array as its value, and each element in the child array holds a non-array variable - we can store much more information. Consider this script:

<?php
    $capitalcities['England'] = array("Capital"=>"London", "Population"=>40000000, "NationalSport"=>"Cricket");
    $capitalcities['Wales'] = array("Capital"=>"Cardiff", "Population"=>5000000, "NationalSport"=>"Rugby");
    $capitalcities['Scotland'] = array("Capital"=>"Edinburgh", "Population"=>8000000, "NationalSport"=>"Football");
    var_dump($capitalcities);
?>

This time we're creating our $capitalcities array elements as before, but we're using an array for each value. Each child array has three elements: Capital, Population, and NationalSport. At the end there is a var_dump() for the parent array, which should really show you how helpful var_dump() is. Here is the output:

array(3) {
    ["England"]=>
    array(3) {
        ["Capital"]=>
        string(6) "London"
        ["Population"]=>
        int(40000000)
        ["NationalSport"]=>
        string(7) "Cricket"
    }
    ["Wales"]=>
    array(3) {
        ["Capital"]=>
        string(7) "Cardiff"
        ["Population"]=>
        int(5000000)
        ["NationalSport"]=>
        string(5) "Rugby"
    }
    ["Scotland"]=>
    array(3) {
        ["Capital"]=>
        string(9) "Edinburgh"
        ["Population"]=>
        int(8000000)
        ["NationalSport"]=>
        string(8) "Football"
    }
}

As you can see, not only does var_dump() recurse into child arrays to output their contents to, but it indents all the output according to the array level.

Think of multidimensional arrays as allowing you to store arrays of arrays, because that is their most common purpose. Of course, they can also store arrays of arrays of arrays and so on, but it is all the same.

Author's Note: The count() function has a helpful second parameter that, when set to 1, makes count() perform a recursive count. The difference is that if you pass in a multidimensional array, count() will count all the elements in the first array, then go into the first array element and count all the elements in there, and go into any elements in there, etc, etc. For example, the $capitalcities array above has three elements in, so if you do not use the second parameter to count() you will get 3 back. However, if you pass in 1 for the second parameter, you will get 12: three for the first-level elements ("England", "Wales", "Scotland"), and three each for the variables inside those elements ("Capital", "Population", "NationalSport").

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: The array cursor >>

Previous chapter: Creating an array of numbers

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.