I n this chapter you learn some important skills that improve your work with data. You learn about some more-sophisticated ways to work with arrays and how to manage text information with more flair. Specifically, you learn how to do these things: • Manage arrays with the foreach loop • Create and use associative arrays • Extract useful information from some of PHP’s built-in arrays • Build basic two-dimensional arrays • Build two-dimensional associative arrays • Break a string into smaller segments • Search for one string inside another 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 5 CHAPTER Introducing the Word Search Program Creator By the end of this chapter you can create a fun program that generates word search puzzles. The user enters a series of words into a list box, as shown in Figure 5.1. The program then tries to generate a word search based on the user’s word list. (It isn’t always possible, but the program can usually generate a puzzle.) One pos- sible solution for the word list shown in Figure 5. 1 is demonstrated in Figure 5.2. If desired, the program can also generate an answer key based on the puzzle. This capability is shown in Figure 5.3. The secret to the word find game (and indeed most computer programs) is the way the data is handled. Once I determined a good scheme for working with the data in the program, the actual programming wasn’t too tough. 134 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 FIGURE 5.1 The user enters a list of words and a size for the finished puzzle. Using the foreach Loop to Work with an Array As I mention in chapter 4, “Loops and Arrays,” for loops and arrays are natural companions. In fact, PHP supplies a special kind of loop called the foreach loop that makes it even easier to step through each array element. 135 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.2 This puzzle contains all the words in the list. FIGURE 5.3 Here’s the answer key for the puzzle. Introducing the foreach.php Program The program shown in Figure 5.4 illustrates how the foreach loop works. The HTML page is generated by surprisingly simple code: <html> <head> <title>Foreach Demo</title> </head> <body> <? $list = array(“alpha”, “beta”, “gamma”, “delta”, “epsilon”); print “<ul>\n”; foreach ($list as $value){ print “ <li>$value</li>\n”; } // end foreach print “</ul>\n”; ?> </body> </html> 136 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 FIGURE 5.4 Although it looks just like normal HTML, this page was created with an array and a foreach loop. All the values in the list are created in the $list variable using the array function. The foreach loop works a lot like a for loop, except it is a bit simpler. The first parameter of the foreach construct is an array—in this case, $list. The keyword as indicates the name of a variable that holds each value in turn. In this case, the foreach loop steps through the $list array as many times as necessary. Each time through the loop, the function populates the $value variable with the current member of the $list array. In essence, this foreach loop works just like the fol- lowing traditional for loop: foreach ($list as $value){ print “ <li>$value</li>\n”; } // end foreach Here’s your traditional for loop: for ($i = 0; $i < length($list); $i++); $value = $list[$i]; print “ <li>$value</li>\n”; } // end for loop The main difference between a foreach loop and a for loop is the presence of the index variable ($i in this example). If you’re using a foreach loop and need to know the current element’s index, use the key() function. The foreach loop can be an extremely handy shortcut for stepping through each value of an array. Since this is a common task, knowing how to use the foreach loop is an important skill. As you learn some other kinds of arrays, you see how to modify the foreach loop to handle these other array styles. Creating an Associative Array PHP is known for its extremely flexible arrays. You can easily generate a number of interesting and useful array types in addition to the ordinary arrays you’ve already made. One of the handiest types is called an associative array. While it sounds complicated, an associative array is much like a normal array. While regular arrays rely on numeric indices, an associative array has a string index. Figure 5.5 shows a page created with two associative arrays. TRICK 137 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 . the foreach loop • Create and use associative arrays • Extract useful information from some of PHP s built-in arrays • Build basic two-dimensional arrays • Build two-dimensional associative arrays •. mention in chapter 4, “Loops and Arrays,” for loops and arrays are natural companions. In fact, PHP supplies a special kind of loop called the foreach loop that makes it even easier to step through. the words in the list. FIGURE 5.3 Here’s the answer key for the puzzle. Introducing the foreach .php Program The program shown in Figure 5.4 illustrates how the foreach loop works. The HTML page