Arrays are very useful structures for storing various kinds of data into the computer's memory. Normal arrays are much like lists. Associative arrays are like name/value pairs. A third special type of array acts much like a table of data. For instance, imagine you were trying to write a program to help users determine the distance between major cities. You might start on paper with a table like Table 5.1:
? |
Indianapolis |
New York |
Tokyo |
London |
---|---|---|---|---|
Indianapolis |
0 |
648 |
6476 |
4000 |
New York |
648 |
0 |
6760 |
3470 |
Tokyo |
6476 |
6760 |
0 |
5956 |
London |
4000 |
3470 |
5956 |
0 |
It's reasonably common to work with this sort of tabular data in a computer program. PHP (and most languages) provides a special type of array to assist in working with this kind of information. The basicMultiArray program featured in Figures 5.8 and 5.9 illustrates how a program can encapsulate a table.
Using a two-dimensional array is actually pretty easy if you plan well. I first wrote out my table of data on paper (actually, I have a white board in my office for exactly this kind of situation). I assigned a numeric value to each city, so
Indianapolis = 0
New York = 1
Tokyo = 2
London = 3
This will make it easier to keep track of the cities later on.
The HTML code builds the two select boxes and a Submit button in a form.
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Basic multi-dimensional array</title> </head> <body> <h1>Basic 2D Array</h1> <form action = basicMultiArray.php> <table border = 1> <tr> <th>First city</th> <th>Second city</th> <tr> <!-- note each option value is numeric --> <tr> <td> <select name = "cityA"> <option value = 0>Indianapolis</option> <option value = 1>New York</option> <option value = 2>Tokyo</option> <option value = 3>London</option> </select> </td> <td> <select name = "cityB"> <option value = 0>Indianapolis</option> <option value = 1>New York</option> <option value = 2>Tokyo</option> <option value = 3>London</option> </select> </td> </tr> <tr> <td colspan = 2> <input type = "submit" value = "calculate distance"> </td> </tr> </table> </body> </html>
Recall that when the user submits this form, it will send two variables. The cityA variable will contain the value property associated with whatever city the user selected, and cityB will likewise contain the value of the currently selected destination city. I carefully set up the value properties so they would coordinate with each city's numeric index. If the user chooses New York as the origin city, the value of $cityA will be 1, because I decided that New York would be represented by the value 1. The reason I'm giving numeric values is because the information will all be stored in arrays, and normal arrays take numeric indices. (In the next section I'll show you how to do the same thing with associative arrays.)
The PHP code that determines the distance between cities is actually quite simple once the arrays are in place.
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Distance calculator</title> </head> <body> <? $city = array ( "Indianapolis", "New York", "Tokyo", "London" ); $distance = array ( array (0, 648, 6476, 4000), array (648, 0, 6760, 3470), array (6476, 6760, 0, 5956), array (4000, 3470, 5956, 0) ); $result = $distance[$cityA][$cityB]; print "<h3>The distance between "; print "$city[$cityA] and $city[$cityB]"; print " is $result miles.</h3>"; ?> </body> </html>
I have two arrays in this program. The $city array is a completely normal array of string values. It contains a list of city names. I carefully set up the array so the numeric values I assigned to the city would correspond to the index in this array. Remember that array indices usually start with zero, so Indianapolis is zero, New York is one, and so on.
The user won't care that Indianapolis is city 0, so I used the $city array to assign names to the various cities. If the user chose city zero (Indianapolis) for the $cityA field, I can refer to the name of that city as $city[$cityA] because $cityA will contain the value 0 and $city[0] is "Indianapolis."
The distances don't fit in a list, because it requires two values to determine a distance. You must know which city you are coming from and which city you are going to in order to calculate a distance. These two values correspond to rows and columns in the original table. Look again at the code that generates the $distance array.
$distance = array ( array (0, 648, 6476, 4000), array (648, 0, 6760, 3470), array (6476, 6760, 0, 5956), array (4000, 3470, 5956, 0) );
The $distance array is actually an array full of other arrays! Each of the inner arrays corresponds to distance from a certain destination city. For example, since Indianapolis is city 0, the first (zeroth?) inner array refers to the distance between Indy and the other cities. If it helps, you can think of each inner array as a row of a table, and the table as an array of rows. It might sound complicated to build a two-dimensional array, but it actually is more natural than you may think. If you compare the original data in Table 5.1 with the code that creates the two-dimensional array, you'll see that all the numbers are in the right place.
TRICK? |
There's no need to stop at two dimensions. It's possible to build arrays with three, four, or any other number of dimensions. However, it becomes difficult to visualize how the data works with these complex arrays. Generally, one-and two dimensions are as complex as you'll want your ordinary arrays to get. For more complex data types, you'll probably want to look towards file manipulation tools and relational data structures, which you'll learn throughout the rest of this book. |
Once data is stored in a two-dimensional array, it is reasonably easy to retrieve. To look up information in a table, you need to know the row and column. A two-dimensional array requires two indices, one for the row, and one for the column. To find the distance from Tokyo (City number 2) to New York (City number 1), you can simply refer to $distance[2][1]. The code for the demo program gets the index values from the form:
$result = $distance[$cityA][$cityB];
This value is stored in the variable $result and then sent to the user.