PHP Nested Associative Arrays

Programmers

I have a complicated love/hate relationship with PHP nested associative arrays. They are incredibly useful…but only when you can get them to work. Much like mules.

What they are

If you’re new to programming, arrays are like to-do lists. It is one list ( array) made up of many items (variables).

GROCERY LIST
Toothpaste;
Awesome Bedspread
Chocolate

An associative array would be if you had to get each item at a different store. You might write it:

GROCERY LIST
Walmart -> Toothpaste;
Target -> Awesome Bedspread;
Stater Brothers -> Chocolate

Someone could then ask, “what are you getting at walmart?” and you would say, “toothpaste”.

A nested associative array would be if you needed multiple items from one store:

GROCERY LIST
Walmart ->
Item 1 ->Toothpaste
Item 2->Gum
Item 3 ->Candy
Target ->
Item 1 -> Awesome Bedspread
Item 2 -> Elf the movie
Item 3 -> Sherlock Holmes book
etc.

Creating a Nested Associative Array

The idea itself isn’t a very hard concept. The hard part is getting the “grammar” right so that the computer understands what we’re doing. Let’s start with a basic hard-coding of a nested associative array.

 $groceryList = array(
walmart => array(
item1 => toothpaste,
item2 => gum,
item3 => candy
),
target => array(
item1 => bedspread,
item2 => elf,
item3 => sherlock
)
); 

Tips

The ‘$’ variable sign only goes before the grand-daddy array. It doesn’t get added anywhere else, not on the child (walmart) or the grandchild (item1) or the great-grandchild (toothpaste).

The commas go after each item in an array unless it is the last item, just like english grammar. It goes after toothpaste and gum, but not candy because that is the last item and we are ending the sentence with the ‘)’. It goes after the walmart array (notice the ‘),” after candy) because it is the first item in the grocerList array, but not after target because we are ending that sentence with a ‘)’. Note we also end the entire array with a semicolon, as in all items in PHP.

Add ‘array’ after any item that will itself become an array. For example, walmart is becoming an associative array that will include three items and their titles, so you must add ‘array(‘ and add the closing parenthesis ‘)’ to complete the array. Or, to put it more simply, if you only need walmart to refer to one item, then you wouldn’t need a list. It would just be an item. If you need a list, add ‘array’.

You use ‘=>’ in PHP to denote that the array is an associative array. Therefore, you could reference “toothpaste” (and we will!!) by using ‘item1’. If you don’t need to reference it that specifically, you could do:

$groceryList = array( walmart => array( toothpaste, gum, candy), target => array(bedspread, elf, sherlock) );

That makes the walmart array NOT an associative array, but a regular array, because we are not giving toothpaste a head title.

Making it Dynamic!

Most of the time, hard-coding an array won’t be super useful. If you already know what the items are, you don’t really need an array. But dynamic arrays are awesome.

Basically, all you need to know about this is that any part of the equation (walmart, item1, bedspread) can be replaced with variables. You could write:

 $store1 = "walmart";
$item = "office";
$value = "pencils";
$store1 => array( $item => $value);

Adding to the Array

Finally, you’ll need to add to the array while you’re doing all that fancy dynamic coding. Here’s how you do it:

$n = "walmart";
$p = "office";
$v = "pencils";
$array[$n][$p] = $v;

For each nested array, add a pair of brackets ‘[ ]’. So you you are trying to set the item1 to equal toothpaste, you would count each time ‘array’ occurs before ‘toothpaste’, and add that many brackets:

$groceryList[walmart][item1] = "toothpaste";

You could also set it like this:

$groceryList[walmart] = array(item1 => "toothpaste");

which would be the same as:

$array[$n] = array($p => $v);     //(using the same values as above)

Summary

Now that you know the grammar, only your imagination can limit how you use the amazing, the incredible, PHP nested associative arrays.

Comments