153 C h a p t e r 5 B e t t e r A r r a y s a n d S t r i n g H a n d l i n g $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 0, and the distance from Indy to New York is 648, and so on. I created an associative array for each city and 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>”; You can combine associative and normal arrays. It is possible to have a list of associative arrays and put them together in a normal array, or vice versa. PHP’s array-handling capabilities allow for a phenomenal level of control over your data structures. Manipulating String Values The Word Search program featured at the beginning of this chapter uses arrays to do some of its magic, but arrays alone are insufficient for handling the tasks needed for this program. The Word Search program takes advantage of a number of special string manipulation functions to work extensively with text values. PHP has a huge number of string functions that give you an incredible ability to fold, spindle, and mutilate string values. Demonstrating String Manipulation with the Pig Latin Translator As a context for describing string manipulation functions, consider the program featured in Figures 5.10 and 5.11. This program allows the user to enter a phrase into a text box and converts the phrase into a bogus form of Latin. If you’re not familiar with pig Latin, it’s a silly kid’s game. Essentially, you take the first letter of each word, move it to the end of the word, and add ay. If the word begins with a vowel, simply end the word with way. The pigify program uses a number of string functions to manipulate the text: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Pig Latin Generator</title> </head> TRICK TRICK 154 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r <body> <h1>Pig Latin Generator</h1> <? if ($inputString == NULL){ print <<<HERE 155 C h a p t e r 5 B e t t e r A r r a y s a n d S t r i n g H a n d l i n g FIGURE 5.10 The pigify program lets the user type some text into a text area. FIGURE 5.11 The program translates immortal prose into incredible silliness. <form> <textarea name = “inputString” rows = 20 cols = 40></textarea> <input type = “submit” value = “pigify”> </form> HERE; } else { //there is a value, so we’ll deal with it //break phrase into array $words = split(“ “, $inputString); foreach ($words as $theWord){ $theWord = rtrim($theWord); $firstLetter = substr($theWord, 0, 1); $restOfWord = substr($theWord, 1, strlen($theWord)); //print “$firstLetter) $restOfWord <br> \n”; if (strstr(“aeiouAEIOU”, $firstLetter)){ //it’s a vowel $newWord = $theWord . “way”; } else { //it’s a consonant $newWord = $restOfWord . $firstLetter . “ay”; } // end if $newPhrase = $newPhrase . $newWord . “ “; } // end foreach print $newPhrase; } // end if ?> </body> </html> 156 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r Building the Form This program uses a PHP page to create an input form and to respond directly to the input. It begins by looking for the existence of the $inputString variable. This variable does not exist the first time the user gets to the page. In this situation, the program builds the appropriate HTML page and awaits user input. The program runs again after the user hits the submit button, but this time the $inputString variable has a value. The rest of the program uses string manipulation functions to create a pig Latin version of the input string. Using the split() Function to Break a String into an Array One of the first tasks for pigify is to break the entire string that comes from the user into individual words. PHP provides a couple of interesting functions for this purpose. The split() function takes a string and breaks it into an array based on some sort of delimiter, or separator character. The split() function takes two argu- ments. The first argument is a delimiter and the second is a string to break up. I want each word to be a different element in the array, so I use space ( “ “) as a delimiter. The following line takes the $inputString variable and breaks it into an array called $words. Each word is a new array element. $words = split(“ “, $inputString); Once the $word array is constructed, I stepped through it with a foreach loop. I stored each word temporarily in $theWord inside the array. Trimming a String with rtrim() Sometimes when you split a string into an array, each array element still has the split character at the end. In the pig Latin game, there is a space at the end of each word, which can cause some problems later. PHP provides a function called rtrim() which automatically removes spaces, tabs, newlines, and other white- space from the end of a string. I used the rtrim() function to clean off any trail- ing spaces from the split() operation and returned the results to $theWord. $theWord = rtrim($theWord); In addition to rtrim(), PHP has ltrim(), which trims excess whitespace from the beginning of a string and trim(), which cleans up both ends of a string. Also, there’s a variation of the trim commands that allows you to specify exactly which characters are removed. TRICK 157 C h a p t e r 5 B e t t e r A r r a y s a n d S t r i n g H a n d l i n g . possible to have a list of associative arrays and put them together in a normal array, or vice versa. PHP s array-handling capabilities allow for a phenomenal level of control over your data structures. Manipulating. advantage of a number of special string manipulation functions to work extensively with text values. PHP has a huge number of string functions that give you an incredible ability to fold, spindle,. if ?> </body> </html> 156 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r Building the Form This program uses a PHP page to create an input form and to respond directly to the input. It begins by looking for