Exercise 5: Creating Multidimensional Arrays
Multidimensional arrays are both simple and complex at the same time. The structure and concept can be somewhat difficult to grasp, but it is surprisingly easy to create and access multidimensional arrays in PHP.
With a multidimensional array you can create an array that contains more information than a standard array. This is accomplished by using other arrays for values rather than strings and numbers. For example:

This array, $groceries, now consists of one string (peanuts), one floating-point number (30.00), and two arrays (fruits and meats).
Pointing to an element in an array within an array is tricky. The key (pardon the pun) is to continue adding indices in square brackets as necessary. So in this example, bananas is at $groceries[‘fruits’][1].
First, you point to the element (in this case, an array) in the $groceries array, by using [‘fruits’]. Then, you point to the element in that array based on its position. Because it is the second item, you use the index [1].
In this exercise you will write a PHP script for a different scenario. Your task now is to create a multidimensional array containing the chapters for three books.
Note: A number of
Helpful Tips are located at the end of this web page for reference as you complete this exercise.
To use multidimensional arrays:
- You will create a PHP script (Script 7-4):
Script 7-4: The multidimensional $books array stores a lot of information in one big variable.

- To begin, create a new HTML document (Script 7-4) with the title My Books and Chapters:

- Create the initial PHP tags, and address error handling, if necessary:

- Create the first array:

To build up the multidimensional array, you will create three standard arrays and then use them as the values for the larger array. This array (called $janeeyre, which is short for Jane Eyre) uses numbers for the keys and strings for the values. The numbers begin with 1 and correspond to the chapter numbers. The values are the chapter titles.
- Create the next two arrays:

For each array, you only need to add the first four chapters from each book for simplicity’s sake.
- Create the main multidimensional array:

The $books array is the master array for this script. It uses strings for keys (which are shortened versions of the book titles) and arrays for values. Here you are using the array() function to create the array.
- Print out the name of the third chapter of the Jane Eyre book:

Following the rules stated earlier, all you need to do to access any individual chapter name is to begin with $books, follow that with the first index ([‘Jane Eyre’]), and follow that with the next index ([3]). Because you are placing this in a print() statement, you will enclose the whole construct in curly braces to avoid parse errors.
- Print out two more examples:

- Run the $books array through a foreach loop to see the results:

The $key variable stores each abbreviated book title, and the $value variable ends up containing each chapter array.
- Close the PHP section and complete the HTML page:

- Save the script as books.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter7).
- Using Cyberduck, upload the file to the production server, and test in your browser (Figure 7-5), making sure you are looking at the production server version of the file.
Figure 7-5: The first three lines are generated by print() statements. The last three show the results of the foreach loop. As each variable for each book contains an array, notice the word Array appears. See the Tips section lower down on this page for a solution for fixing this issue.

Important: If something doesn’t look right in your document, and you need to make changes to the script, follow the editing procedure using Synapse outlined in
Chapter 1 .

- To access every element of every array, you can nest two foreach loops like this (Figure 7-6):

Figure 7-6: One foreach loop within another can access every element of a multidimensional array.

- Using the print_r() or dump() function (preferably enclosed in HTML <pre> tags for better formatting), you can view an entire multidimensional array (Figure 7-7).
Figure 7-7: The var_dump() function shows the structure and contents of the $books array.

- You can create a multidimensional array in one statement by using a series of nested array() calls (instead of using several steps as in this example). However, doing so is not recommended. This is because it is all too easy to make syntactical errors as a statement becomes more and more nested.
- Although all the sub-arrays in this example have the same structure (numbers for indexes and four elements), such consistency is quite unusual with multidimensional arrays. Real-world examples will vary greatly.
← Return to Chapter 7, Assignments Page