while ( list( $product, $price ) = each( $prices ) ) We can loop through the entire $prices array, echoing the contents The array returned by each() into two new variables called ItĪlso uses list() to turn the 0 and 1 elements from $prices, return it as an array, and make the next element current. This line uses each() to take the current element from Like this: $list( $product, $price ) = each( $prices ) We can separate two of the values that the each() function gives us Theįunction list() can be used to split an array into a number of values. There is a more elegant and more common way of doing the same thing. Although it makes no difference which youĬhoose, we have chosen to use the named locations, rather than the numbered Of the current element, and the locations value and 1 contain When we callĮach(), it gives us an array with four values and the four indexes to In this code, the variable $element is an array. Because we are calling each() withinĪ while loop, it returns every element in the array in turn and stops This function returns the current element in an array and makes The preceding code uses the each() function, which we have In Chapter 1, we looked at while loops and the echo The output of this script fragment is shown in Figure 3.2.įigure 3.2 An each() statement can be used to loop through The following code lists the contents of our $prices array using theĮach() construct: while( $element = each( $prices ) ) We can incorporate the keys as well: foreach ($prices as $key => $value) We can use it exactly as we did in the previous example, or The foreach loop has a slightly different structure when usingĪssociative arrays. We can use theįoreach loop or the list() and each() constructs. $prices = 4 Using Loops with Associative Arraysīecause the indices in this associative array are not numbers, we cannot useĪ simple counter in a for loop to work with the array. Version, we do not explicitly create an array at all. Here is another slightly different, but equivalent piece of code. Rather thanĬreating an array with three elements, this version creates an array with only The following code will create the same $prices array. Like numerically indexed arrays, associative arrays can be created and 'Oil'=>10, 'Spark Plugs'=>4 ) Accessing the Array ElementsĪgain, we access the contents using the variable name and a key, so we canĪccess the information we have stored in the prices array as $prices[ The following code creates an associative array with product names as keysĪnd prices as values. In an associative array, we canĪssociate any key or index we want with each value. This meant that the first item we added became item 0, the second item 1, and so In the products array, we allowed PHP to give each item the default index. Read PHP and MySQL® Web Development, Fourth Edition or more than 24,000 other books and videos on Safari Books Online. PHP and MySQL Web Development, 2nd Edition
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |