JavaScript Editor Javascript validator     Website design 



Team LiB
Previous Section Next Section

Making a Two-Dimensional Associative Array

You can also create two-dimensional associative arrays. It takes a little more work to set up this array, but it can be worth it because the name-value relationship eliminates the need to keep track of numeric identifiers for each element. Another version of the multiArray program illustrates how to use associative arrays to generate the same city distance program.

TRICK 

Since this program looks exactly like the basicMultiArray program to the user, I am not showing the screen shots. All of the interesting features of this program are in the source code.

Building the HTML for the Associative Array

The HTML page for the associative version of this program 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>
      <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 properties of the select objects. In this case, the distance array will be an associative array, so it will not have numeric indices. Since the indices can be text-based, I send the actual city name as the value for $cityA and $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>
</head>
<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
  );

$result = $distance[$cityA][$cityB];
print "<h3>The distance between $cityA and $cityB is $result miles.</h3>";

?>

</body>
</html>

Building the Two-Dimensional Associative Array

The basic approach to building a two-dimensional array is the same whether it's a normal array or uses associative indexing. Essentially, you create each row as an array, and then build an array of the existing arrays. In the traditional array, the indices were automatically created. The development of an associative array is a little more complex, because you need to specify the key for each value. As an example, look at the code used to generate the $indy array:

$indy = array (
  "Indianapolis" => 0,
  "New York" => 648,
  "Tokyo" => 6476,
  "London" => 4000
  );

Inside the array, I used city names as indices. The value for each index refers to the distance from the current city (Indianapolis) to the particular destination. The distance from Indianapolis to Indianapolis is zero, and the distance from Indy to New York is 648, and so on.

I created an associative array for each city, and then put those associative arrays together in a kind of mega-associative array:

//set up master array
$distance = array (
  "Indianapolis" => $indy,
  "New York" => $ny,
  "Tokyo" => $tokyo,
  "London" => $london
  );

This new array is also an associative array, but each of its indices refers to an array of distances.

Getting Data from the Two-Dimensional Associative Array

Once the two-dimensional array is constructed, it's extremely easy to use. The city names themselves are used as indices, so there's no need for a separate array to hold city names. The data can be output in two lines of code:

$result = $distance[$cityA][$cityB];
print "<h3>The distance between $cityA and $cityB is $result miles.</h3>";

TRICK 

If you wish, you can combine associative and normal arrays. It would be possible to have a list of associative arrays and put them together in a normal array, or vice-versa. PHPs array-handling capabilities allow for a phenomenal level of control over your data structures.


Team LiB
Previous Section Next Section


JavaScript Editor Javascript validator     Website design


R7