HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [219]
♦ You must specify a key. If you’re building a numeric-indexed array, PHP can always guess what key should be next. This isn’t possible with an associative array.
♦ Associative arrays are best for name-value pairs. Associative arrays are used when you want to work with data that comes in name/value pairs. This comes up a lot in PHP and XHTML. XHTML attributes are often in this format, as are CSS rules and form input elements.
♦ Some of PHP’s most important values are associative arrays. The $_REQUEST variable (described in Chapter 3 of this minibook) is an important associative array. So are $_GET, $_POST, and several others.
Make sure to include quotation marks if you’re using a string as an array index. It will probably work if you don’t, but it’s bad programming practice and may not work in the future.
Using foreach with associative arrays
It’s very common to have a large associative array that you want to evaluate. For example, PHP includes a very useful array called $_SERVER that gives you information about your server configuration (things like your hostname, PHP version, and lots of other useful stuff). The following code snippet (from serverInput.php) runs through the entire $_SERVER array and prints each key/value pair:
print “
- \n”;
- $key
- $value
foreach ($_SERVER as $key => $value){
print << HERE; } // end foreach print “
?>
You can see this program running on my work server in Figure 4-5.
Figure 4-5: This variable stores data in an associative array.
Here’s how it works:
1. Begin the foreach loop as normal.
The associative form of the foreach loop begins just like the regular one:
foreach ($_SERVER as $key => $value){
2. Identify the associative array.
The first parameter is the array name:
foreach ($_SERVER as $key => $value){
3. Create a variable for the key.
Each element of an associative array has a key and a value. I put the key in a variable named $key:
foreach ($_SERVER as $key => $value){
4. Use the => symbol to indicate the associative relationship.
This symbol helps PHP recognize you’re talking about an associative array lookup:
foreach ($_SERVER as $key => $value){
5. Assign the value of the element to a variable.
The $value variable holds the current value of the array item:
foreach ($_SERVER as $key => $value){
6. Use the variables inside your loop.
Each time PHP goes through the loop, it pulls another element from the array, puts that element’s key in the $key array, and puts the associated value in $value. You can then use these variables inside the loop however you wish. I used a definition list because it’s a natural way to display key-value pairs. A list of definitions is keys and values.
print << HERE; The $_SERVER variable is extremely useful for checking your environment, but you shouldn’t make a program that displays this kind of information available on a publicly accessible server. Doing so gives the bad guys information they could use to cause you headaches. Use it for testing and debugging, and then remove it. I have this example disabled on my site, but you can still look at the source code if you wish. Arrays in PHP can hold anything, even other arrays. This turns out to be an extremely useful function. A multidimensional array is an array that holds arrays. Multidimensional arrays are used when your data is arranged in some sort of tabular form. Some uses for these are to group things or to use as lookup tables. See Book IV, Chapter 4 for one possible use of lookup tables — using multidimensional arrays to hold the distances between cities. You can do exactly the same thing with PHP. Even though the syntax is somewhat different, the concept is exactly the same. Figure 4-6 is an HTML page that lets the user choose what city she is traveling from and to. Figure 4-6: The
Introducing Multidimensional Arrays
We’re going on a trip