The $cityarray is a com-pletely normal array of string values.. I set up the array so the numeric values I assigned to the city would correspond to the index in this array.. Remember tha
Trang 1$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>
Storing City Names in the $city Array
I have two arrays in this program, $cityand $distance The $cityarray is a com-pletely normal array of string values It contains a list of city names I 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 0, so Indianapolisis
0, New Yorkis 1, and so on
The user won’t care that Indianapolisis city 0, so the $cityarray assigns names
to the various cities If the user chose city 0(Indianapolis) for the $cityAfield, I can refer to the name of that city as $city[$cityA]because $cityAcontains the value 0and $city[0]is Indianapolis
Storing Distances in the $distance Array
The distances don’t fit into a regular list, because it requires two values to deter-mine a distance You must know from which city you are coming and going to
148
g r
s o
l u
g in
e r
Trang 2calculate a distance These two values correspond to rows and columns in the
original table Look again at the code that generates the $distancearray:
$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
Indianapolisis 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 is more
nat-ural than you may think If you compare the original data in Table 5.1 with the
code that creates the two-dimensional array, you see that all the numbers are in
the right place
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 ordinary arrays should get For more complex data types, look toward file-manipulation tools and relational data structures, which you learn throughout the rest of this book.
Getting Data from the $distance Array
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),
sim-ply refer to $distance[2][1] The code for the program gets the index values from
the form:
$result = $distance[$cityA][$cityB];
This value is stored in the variable $resultand then sent to the user
T R I C K
149
l i
Trang 3Making a Two-Dimensional
Associative Array
You can also create two-dimensional associative arrays It takes a little more work
to set it up, but can be worth it because the name/value relationship eliminates the need to track numeric identifiers for each element Another version of the multiArrayprogram illustrates how to use associative arrays to generate the same city-distance program
Since this program looks exactly like the basicMultiArray program to the user,
I am not showing the screen shots All of this program’s interesting features are in the source code.
Building the HTML for the Associative Array
The HTML page for this program’s associative version is much like the indexed version, except for one major difference See if you can spot the difference in the source code:
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>2D Array</title>
</head>
<body>
<h1>2D Array</h1>
<form action = multiArray.php>
<table border = 1>
<tr>
<th>First city</th>
<th>Second city</th>
<tr>
<!— note each option value is a string —>
<tr>
<td>
<select name = “cityA”>
<option value = “Indianapolis”>Indianapolis</option>
<option value = “New York”>New York</option>
T R I C K
150
g r
s o
l u
g in
e r
Trang 4<option value = “Tokyo”>Tokyo</option>
<option value = “London”>London</option>
</select>
</td>
<td>
<select name = “cityB”>
<option value = “Indianapolis”>Indianapolis</option>
<option value = “New York”>New York</option>
<option value = “Tokyo”>Tokyo</option>
<option value = “London”>London</option>
</select>
</td>
</tr>
<tr>
<td colspan = 2>
<input type = “submit”
value = “calculate distance”>
</td>
</tr>
</table>
</body>
</html>
The only difference between this HTML page and the last one is the value
prop-erties of the selectobjects In this case, the distance array is an associative array,
so it does not have numeric indices Since the indices can be text based, I send
the actual city name as the value for $cityAand $cityB
Responding to the Query
The code for the associative response is interesting, because it spends a lot of
effort to build the fancy associative array Once the array is created, it’s very easy
to work with
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>Distance Calculator</title>
151
l i
Trang 5<body>
<h1>Distance Calculator</h1>
<?
//create arrays
$indy = array (
“Indianapolis” => 0,
“New York” => 648,
“Tokyo” => 6476,
“London” => 4000
);
$ny = array (
“Indianapolis” =>648,
“New York” => 0,
“Tokyo” => 6760,
“London” => 3470
);
$tokyo = array (
“Indianapolis” => 6476,
“New York” => 6760,
“Tokyo” => 0,
“London” => 5956
);
$london = array (
“Indianapolis” => 4000,
“New York” => 3470,
“Tokyo” => 5956,
“London” => 0
);
//set up master array
$distance = array (
“Indianapolis” => $indy,
“New York” => $ny,
“Tokyo” => $tokyo,
“London” => $london );
152
g r
s o
l u
g in
e r