State Your Case: Associative Arrays in PHP

The greatest state?
State of Texas
State Selection Form

In TNT, we have an app that lets you select a state and click on links that take you to governmental sites that tell you information about that state. An 'associative array' in PHP was used to store 'key-value' pairs of state names (keys) with their abbreviations (values). Here's the array that was in a P-COW as the page loaded:

$stateInfo = array(
    'New Hampshire'=>'NH',
    'New Jersey'=>'NJ',
    'New Mexico'=>'NM',
    'New York'=>'NY',
    'North Carolina'=>'NC',
    'North Dakota'=>'ND',
    'Rhode Island'=>'RI',
    'South Carolina'=>'SC',
    'South Dakota'=>'SD',
    'West Virginia'=>'WV',

This array was used to build a 'drop down' menu dynamically in PHP. Here's the code for the form:

<form name="selectState" method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <legend>Choose a state:</legend>
        <label for="stateAbbrev">State:</label>
        <select name="stateAbbrev" id="stateAbbrev">
                //assumes the 'stateInfo' associative array is in the mind of the main page
                $maxNum = sizeof($stateInfo);
                $theStates = array_keys($stateInfo);
                $stateAbbreviations = array_values($stateInfo);
                for($i = 0; $i<$maxNum; $i++){
                    $myState = $theStates[$i];
                    if($myState == $stateName)
                        echo "<option value=\"$stateAbbreviations[$i]\"
                        echo "<option value=\"$stateAbbreviations[$i]\">$myState</option>";
      <input type="submit" name="submit" id="submit" value="Submit">


admin's picture

I may be state-ing the obvious, but this code is important because it shows how to build a menu dynamically if you have data in an array. As 2nd-year web design students begin putting information in a mySQL data base, they may find that they need to make menus out of that data. Hmmm...

  1. Store information in a mySQL database
  2. Establish a link (connection) to the database
  3. Formulate a query to get the information you want as a data set
  4. Read the data into a PHP array
  5. Create the select menu using that array information as shown in this post!


admin's picture

If you run the original app, you will notice that the select menu 'pre-populates' with 'Texas.' How? In the P-COW before the page loads (above the doctype) we have the following code snippet:

    $stateAbbrev = $_POST['stateAbbrev'];
    $stateName= array_search($stateAbbrev, $stateInfo);
    $stateName = "Texas";

Notice that before the page is submitted, the variable '$stateName' is assigned to Texas. When the loop generates the menu items, the selection logic causes the attribute 'selected' to be added for the entry Texas

admin's picture

In the snippets above, notice how I aligned the array entries. Although it meant a long verticle chunk of code, it made viewing the information easier from an editting point of view. Also, notice the form code: nested items are indented, making it easier to find problems if they occur