Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays Wish List | Report Bug | About Me ]

5.7     Multidimensional arrays

This is NOT the latest copy of this book; click here for the latest version.

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").





<< 5.6.11 Creating an array of numbers: range()   5.8 The array cursor: reset(), end(), next(), and prev() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
steve - 07 Sep 2008

im very new to php and im an application designer..i want to the quickest way of learning php hopefully b4 i get fired....how do you link two tables in php, sounds easy i know but i can only do it with relational calculus..can some help?it might be thre wrong place to ask for this info though

Matt - 07 Sep 2008

"I searched all over to find this and I am surprised it was not here. If you are using multidimensional arrays and you wish to access a element in it without going through a for each or while you can by array[0][0]"

good point, that was my guess, but it doesn't actually say.

new to php - 07 Sep 2008

I searched all over to find this and I am surprised it was not here. If you are using multidimensional arrays and you wish to access a element in it without going through a for each or while you can by array[0][0]

A PHP User - 07 Sep 2008

It would bw nice to have an output example that show how to output a multi-dimensional array as nested html lists or something similar



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow