To complete the templatizing assignment, you will need to have a good understanding of arrays. This post is meant to be read in addition to the readings from the assignment post, to give you an introductory description of arrays to help you think about arrays clearly.
Simple arrays
An array is a list of data. In its simplest form, each element in the list consists of two bits of data: a key and a value. So you can think of an array as a table with two columns: one for the key, and the other for the value of that element in the array.
For example, we could conceptually think of an array containing a shopping list as follows:

An array holding data representing a shopping list
How to create a simple array in PHP
In PHP, we would create this array using the built-in PHP array() function as follows:
$shoppingList = array(
"potatos",
"tomatoes",
"2% milk",
"prune butter",
"organic muesli",
"eggs",
"unsalted butter",
"half-sour pickles",
"shallots",
"bananas"
);
Notice that we never have to explicitly state what the key for each row is. If we don’t specify the key, it is automatically filled in with a number. The first element always has key 0, the second has the key number 1, the third has a key of 3, etc.
So the eighth element in the list, “half-sour pickles”, is automatically assigned a key with the number 7, and so on.
Accessing the elements of an array in PHP
If we wanted to echo the value of the 8th element in the list, we could use the following PHP command:
echo $shoppingList[7];
This would output the text “half-sour pickles”.
If we wanted to add another element containing the word, “hand soap”, to the end of the existing list of elements in the 11th position, we could use the following PHP code:
$shoppingList[10] = “hand soap”;
Alternatively, the following code will also add an element to the end of the existing list of elements in the array:
$shoppingList[] = “hand soap”;
The advantage of this latter code is that it does not require us to hard-code the number to use as the key for the new element in the array. This makes it a more reusable and flexible technique for adding an element to the end of an array.
Assigning custom keys
Alternatively, we could have explicitly specified the keys we wanted to use for each element in the array by using code like this:
$shoppingList = array(
9 => "potatos",
1 => "tomatoes",
2 => "2% milk",
8 => "prune butter",
3 => "organic muesli",
7 => "eggs",
4 => "unsalted butter",
6 => "half-sour pickles",
5 => "shallots",
0 => "bananas"
);
In this example, we have overriden the default automatically incrementing key numbering system and are specifying keys in whatever order we like.
Associative arrays
In fact, if we wanted to, we could use strings for keys instead of integers. This is done in more or less the same was as we just saw used for assigning custom keys to an array.
For example, let’s say we wanted to create an array that held the grades of the students in a class. We could link up a student’s first name and grade as outlined in this diagram:

An associative array
How to create an associative array in PHP
To create an associative array in PHP, based on the diagram above, we would use the following code:
$grades = array(
"Amos" => "A",
"Jack" => "A",
"Susan" => "B",
"Donny" => "C",
"Michael" => "C",
"Joshua" => "F"
);
Accessing the elements of an associative array in PHP
If we wanted to echo the grade for Susan, we could use the following code:
echo $grades["Susan"];
If we wanted to add a new element to this array to hold Luis’s grade, a B+, we could used the following code:
$grades["Luis"] = "B+";
Debugging arrays in PHP
If you are working with arrays in PHP and are having problems, it often helps to output the contents of the array using the built-in print_r() function of PHP.
For example, to output the array containing grades that we created above, we could use the following code:
print_r($grades);
Running this code will output the following text to the browser:
Array
(
[Amos] => A
[Jack] => A
[Susan] => B
[Donny] => C
[Michael] => C
[Joshua] => F
[Luis] => B+
)
This will allow us to easily see the values that are stored inside the array. And we can use this information to check to see if the values we intended to store in the array are indeed being stored there properly.
Multidimensional arrays
Let’s now imagine that we wanted to store a list of data that had more than just a key and a value. For example, let’s say we had a list of our favorite classical symphonies, structured as a sort of table of data:

A multidimensional array
This has more than just one value associated with each key. In this case, each key has a list of data associated with it, including “title”, “composer”, “key” and “year” values. In other words, there is a sub-array of data associated with each key.
How to create a multidimensional array in PHP
So, in order to create such a multidimensional array of data, we create an array filled with arrays:
$symphonies = array(
array(
"title" => "Symphony #1",
"composer" => "Jean Sibelius",
"key" => "E minor",
"year" => "1898"
),
array(
"title" => "Symphony in C major",
"composer" => "Richard Wagner",
"key" => "C major",
"year" => "1832"
),
array(
"title" => "Symphony #7",
"composer" => "Ludwig van Beethoven",
"key" => "A major",
"year" => "1811"
),
array(
"title" => "Symphony #3",
"composer" => "Anton Bruckner",
"key" => "D minor",
"year" => "1873"
),
array(
"title" => "Symphony #10",
"composer" => "Dmitri Shostakovich",
"key" => "E minor",
"year" => "1953"
),
array(
"title" => "Symphony #9",
"composer" => "Antonin Dvořák",
"key" => "E minor",
"year" => "1893"
)
);
In many ways, this array is not so different from the simple shopping list array we created at the beginning of this tutorial. We have not specified what keys to use for each element in the array, so PHP automatically assigns incrementing integers as the keys.
It just so happens that each element in the list is an array, rather than some text or a number. That’s why it is called a multidimensional array.
Accessing the elements of an associative array in PHP
To read the values contained within the array, we must bear in mind that we have two arrays. In our example above of the symphonies, the outer array is a simple array whose keys are integers. The inner arrays are associative arrays with strings as keys.
To print out the contents of the “composer” field of the third element in the array of symphonies, we could use the following code:
echo $symphonies[2]["composer"];
This would output the text:
Ludwig van Beethoven
And if we wanted to add a new symphony to the list, we could use code like the following:
$symphonies[] = array(
"title" => "Symphony #3",
"composer" => "Gustav Mahler",
"key" => "D minor",
"year" => "1893"
);
Notice that we are not specifying the key for this new element of the array, so PHP automatically assigns it the next available integer, in this case 6.
Looping through simple arrays
Often, in programming, we want to loop through the elements in an array. The built-in foreach() function in PHP is very useful for this purpose.
To loop through a simple array, such as the shopping list array we created at the beginning of this tutorial, we can use the foreach loop in the following code. This will output the value of each element in the array:
foreach ($shoppingList as $item) {
echo $item . "<br />";
}
Looping through associative arrays
When you have an associative array, as in the student grades example above, you will often be interested not only in the value of each element of the array, but also in its custom key. To access both the key and the value of each element as you loop through the array, you can use a foreach loop like the following:
foreach ($grades as $name => $grade) {
echo $name . " got a " . $grade . "<br />";
}
This effectively goes through each element in the $grades array one at a time, and divides it up into two variables: $name and $grade. $name holds the key, and $grade holds the value. This loop iterates through each element in the array, and results in the outputting of the following text:
Amos gets a A<br />
Jack gets a A<br />
Susan gets a B<br />
Donny gets a C<br />
...
… and so on.
Looping through multidimensional arrays
When dealing with multidimensional arrays, as we have seen, it is often the case that you have a list of associative arrays within a simple array.
When this is the case, such as with the array of symphonies in the examples above, we can use the following code to loop through and output contents of the multidimensional array:
foreach ($symphonies as $symphony) {
echo $symphony["composer"] . " composed " . $symphony["title"] . " in " . $symphony["year"] . "<br />";
}
This code iterates through each element in the $symphonies multidimensional array, and puts the value of each element into a variable called $symphony. Recall that each element of the $symphonies array is actually an array in its own right. So for each iteration of the loop, the $symphony variable holds an associative array containing just the data for that particular symphony.
We can then access the values held within this $symphony array the same way we access values held within any associative array: by using the proper keys. The end result is the output of text like this:
Jean Sibelius composed Symphony #1 in 1898<br />
Richard Wagner composed Symphony in C major in 1832<br />
Ludwig van Beethoven composed Symphony #7 in 1811<br />
This technique of looping through multidimensional arrays will come in handy when we begin to deal with databases.