PHP Fundamentals: Multidimensional Arrays

Published on February 09, 2014 under How-To, Web Development

Multidimensional Array

Last time, I showed you the basics of an array. Mainly in how to set up one, store information in it, change said information, and display it for your needs. I also showed you how to cycle through an array using a loop for more easier global edits.

While arrays are useful for storing information temporarily, they are still limited in how much. Just a primary key and one piece of information per key. So what if you needed more? What if you wanted to store, let’s say, three pieces of information per key? Granted you could try some kind of "comma separated values" in the entries, like this:

$ourTeam = array();

$ourTeam["men"] = "Tom,Dick,Harry";
$ourTeam["women"] = "Jane,Sarah,Mary";

With this logic though, you still need scripting to separate down the CSV value into useable information. While it’s feasible, it’s not very practical or efficient. A better answer would be a Multidimensional Array, and I’m going to show you how to use them with PHP.

What exactly is a Multidimensional Array?

In laymen’s terms, a multidimensional array is when you have a "parent" array, and instead of putting in single pieces of data, you instead put "child" arrays in as the data entries.

So let’s try that array example again as a multidimensional:

$ourTeam = array();

$ourTeam["men"] = array("Tom", "Dick", "Harry");
$ourTeam["women"] = array("Jane", "Sarah", "Mary");

It’s that simple. Had I not used "men" and "women" as the keys in $ourTeam, then we would have been the standard "0" and "1". In the arrays used for the names, they have these numeric keys. However, you could set up specific keys in those child arrays. Let’s say we wanted to have the ages of the members, with their names as keys:

$ourTeamAges = array();

$ourTeamAges["men"] = array("Tom" => "28", "Dick" => "23", "Harry" => "24");
$ourTeamAges["women"] = array("Jane" => "21", "Sarah" => "26", "Mary" => "25");

Now I could have declared names for the two child arrays used, but I’ll be honest, it’ll just make life more complicated when you want to pull the data. As you’ll see in how to pull data, you really only need a name for the parent array.

Working with data in a multidimensional array

So we’ve gotten into creating multidimensional arrays. Now what if you created one and wanted to add more data to the arrays inside? Let’s say we’re adding two more team members and their ages to our previous example. It would look like this:

$ourTeamAges["men"]["John"] = "25";

$ourTeamAges["women"]["Tina"] = "24";

Notice the use of brackets this time. That’s so we’re adding on to the existing child arrays, as opposed to creating a new array in its place.

Now what if you wanted to alter a piece of data? Say Dick comes in and says his age is actually 22. You would use a system of double sets of brackets:

$ourTeamAges["men"]["Dick"] = "22";

If you want to examine the anatomy this, the first set of brackets calls upon the entry titled men in the parent array, and the second goes into men and locates the Dick entry in its child array, changing the final piece of data.

That was an example of arrays with specific keys, but here’s one with the standard numeric keys, so you see it from all sides:

$farmStand = array();

$farmStand[] = array("apples", "grapes", "oranges", "lemons");
$farmStand[] = array("lettuce", "carrots", "celery", "tomatoes");

So let’s say now you wanted to change tomatoes to peppers. Here’s what it would look like:

$farmStand[1][3] = "peppers";

Remember, in an array, the first entry is 0, hence why the numbers appear as they are. There is no standard if you should use numbers of specific keys, but it really comes down to your needs and how you want things to operate. As an aside, you could go in deeper, and thus end up with an array within an array within an array:

$tooManyArrays["a"]["b"]["c"] = "d";

I honestly would not recommend this. If you have to go beyond parent and child, then it’s time to think XML, JSON, or even database calls. Don’t make your life harder if you don’t need to.

Just like with basic arrays, erasing whole arrays or individual entries is the same as before, using unset:

// Delete Harry from ourTeam

// Clear out all the men from ourTeamAges

// Empty out farmStand

Displaying data from a multidimensional array

When it comes time to actually use your arrays to pull or display data, it’s really not that different than when you pulled data from a basic array. However, like you saw with editing data, you need to use two sets of brackets to call upon entries in the parent and child arrays:

echo "<h2>Our Starting Lineup:</h2>n";

echo "<ul>n";
echo "<li>" . $ourTeam["men"][1] . "</li>";
echo "<li>" . $ourTeam["women"][0] . "</li>";
echo "<li>" . $ourTeam["women"][2] . "</li>";
echo "<li>" . $ourTeam["men"][0] . "</li>";
echo "<li>" . $ourTeam["women"][1] . "</li>";
echo "</ul>n";

It’s a simple list, but you can see how I called up and displayed specific entries out of child arrays simply by identifying the parent and child keys of those entries.

Using FOREACH on a multidimensional array

When it comes to a point that you would want to globally alter the entries of a multidimensional array, a FOREACH loop can still do the job. However, since you’re really dealing with an array within an array, you need to layer your loops.

Let’s add 1 year to everyone’s ages in $ourTeamAges:

foreach ($ourTeamAges as $genders) {
   foreach ($genders as $ages) {
      $ages = $ages + 1;

It’s not much different than when you put an if-else statement into another if-else statement or while loop. You simply layer down until you find the data you wish to change.

Well that sums things up here for arrays in PHP. I’ve included example files so you can see all of this in action. Please feel free to comment or email me if you have questions.

Download example file Download the example file from this article

Tags: php fundamentals, php, array, data

comments powered by Disqus