[ Pobierz całość w formacie PDF ]
.5 ),4: array( name=>"Tricorder", price=>55.5 ),5: array( name=>"ORAC AI", price=>2200.5 ),6: array( name=>"Sonic Screwdriver", price=>22.5 )7: );8: function priceCmp( $a, $b )9: {10: if ( $a[price] == $b[price] )11: return 0;12: if ( $a[price]We define an array called $products, which we want to sort by the price field of each309of its values.We then create the custom sort function, priceCmp().This function accepts twoarguments, $a and $b.These arguments will hold two the arrays that make up thesecond level of the $products array.We compare their price elements, returning 0if the prices are the same, -1 if the first is less than the second, and 1 otherwise.Having defined both function and array, we call the usort() function, passing it the$products array and the name of our comparison function.usort() calls our functionrepeatedly, passing it elements from the $products array and switching the order ofthe elements according to the return value it receives.Finally, we loop through the array, to demonstrate our new ordering.Use usort() for sorting numerically indexed arrays.If you want to apply a similarcustom sort to an associative array, use uasort().uasort() will sort maintaining theassociation between keys and values.Listing 16.2 sorts an associative array usinguasort().Listing 16.2: Using uasort() to Sort a Multdimensional Associative Array byOne of Its Fields1: array( color =>"red", price=>4500.5 ),4: "Tricorder" => array( color =>"blue", price=>55.5 ),5: "ORAC AI" => array( color =>"green", price=>2200.5 ),6: "Sonic Screwdriver" => array( color =>"red", price=>22.5 )7: );8: function priceCmp( $a, $b )9: {10: if ( $a[price] == $b[price] )11: return 0;12: if ( $a[price] $val )18: print "$key: $val[price]\n";19: ?>310You can custom sort an associative array by its keys using the function uksort().uksort() is exactly the same in principle as usort() and uasort(), except that uksort()compares the keys of the array.Listing 16.3 uses uksort() to sort an array by the number of characters in each key.Looking ahead to the next hour, we use the function strlen() to ascertain the lengthin characters of each key.strlen() requires a single string argument and returns aninteger representing the number of characters in the string.Listing 16.3: Using uksort() to Sort an Associative Array by the Length ofIts Keys1: 4,4: xxx => 5,5: xx => 7,6: xxxxx => 2,7: x => 88: );9: function priceCmp( $a, $b )10: {11: if ( strlen( $a ) == strlen( $b ) )12: return 0;13: if ( strlen( $a ) $val )19: print "$key: $val\n";20:21: // output:22: // x: 823: // xx: 724: // xxx: 525: // xxxx: 426: // xxxxx: 227:28: ?>311SummaryIn this hour, you got your hands dirty with some of the more advanced aspects ofworking with arrays and data types.You learned what happens when you castcomplex data types to scalars and vice versa.You learned how PHP deals withdifferent data types in an expression, automatically casting one of them for you.Youlearned about functions, such as is_array(), that test for specific data types, andfunctions, such as intval(), that convert data to a specific type.You learned aboutthe traditional way of traversing an array in PHP using each() and list().You learnedhow to check that a value exists in an array with in_array() and how to remove anelement from an array with unset().You learned how to transform an array witharray_walk().Finally, you learned about the custom sort functions with usort(),uasort(), and uksort().Q&AQ Have we now covered every array function that PHP4 provides?A No, there are even more array functions than we have space to cover in this book!You can see them all listed and described athttp://www.php.net/manual/ref.array.php.WorkshopThe Workshop provides quiz questions to help you solidify your understanding of thematerial covered.Try to understand the quiz answers before continuing to the nexthour's lesson.Quiz answers are provided in Appendix A
[ Pobierz całość w formacie PDF ]