82 Chapter 3 Using Arrays Sorting Multidimensional Arrays Sorting arrays with more than one dimension, or by something other than alphabetical or numerical order, is more complicated. PHP knows how to compare two numbers or two text strings, but in a multidimensional array, each element is an array. PHP does not know how to compare two arrays, so you need to create a method to compare them. Most of the time, the order of the words or numbers is fairly obvious—but for compli- cated objects, it becomes more problematic. User Defined Sorts Here is the definition of a two-dimensional array we used earlier.This array stores Bob’s three products with a code, a description, and a price for each. $products = array( array( 'TIR', 'Tires', 100 ), array( 'OIL', 'Oil', 10 ), array( 'SPK', 'Spark Plugs', 4 ) ); If we sort this array, what order will the values end up in? Because we know what the contents represent, there are at least two useful orders.We might want the products sort- ed into alphabetical order using the description or by numeric order by the price. Either result is possible, but we need to use the function usort() and tell PHP how to com- pare the items.To do this, we need to write our own comparison function. The following code sorts this array into alphabetical order using the second column in the array—the description. function compare($x, $y) { if ( $x[1] == $y[1] ) return 0; else if ( $x[1] < $y[1] ) return -1; else return 1; } usort($products, 'compare'); So far in this book, we have called a number of the built-in PHP functions.To sort this array, we have defined a function of our own.We will examine writing functions in detail in Chapter 5,“Reusing Code and Writing Functions,” but here is a brief introduc- tion. We define a function using the keyword function.We need to give the function a name. Names should be meaningful, so we’ll call it compare(). Many functions take parameters or arguments. Our compare() function takes two, one called x and one called y.The purpose of this function is to take two values and determine their order. 05 525x ch03 1/24/03 2:56 PM Page 82 83 Sorting Multidimensional Arrays For this example, the x and y parameters will be two of the arrays within the main array, each representing one product.To access the Description of the array x,we type $x[1] because the Description is the second element in these arrays, and numbering starts at zero.We use $x[1] and $y[1] to compare the Descriptions from the arrays passed into the function. When a function ends, it can give a reply to the code that called it.This is called returning a value.To return a value, we use the keyword return in our function. For example, the line return 1; sends the value 1 back to the code that called the function. To be used by usort(), the compare() function must compare x and y.The function must return 0 if x equals y,a negative number if it is less, and a positive number if it is greater. Our function will return 0, 1, or –1, depending on the values of x and y. The final line of code calls the built-in function usort() with the array we want sorted ($products) and the name of our comparison function (compare()). If we want the array sorted into another order, we can simply write a different com- parison function.To sort by price, we need to look at the third column in the array, and create this comparison function: function compare($x, $y) { if ( $x[2] == $y[2] ) return 0; else if ( $x[2] < $y[2] ) return -1; else return 1; } When usort($products, compare) is called, the array will be placed in ascending order by price. The “u” in usort() stands for “user” because this function requires a user-defined comparison function.The uasort() and uksort() versions of asort and ksort also require a user-defined comparison function. Similar to asort(), uasort() should be used when sorting an associative array by value. Use asort if your values are simple numbers or text. Define a comparison func- tion and use uasort() if your values are more complicated objects such as arrays. Similar to ksort(), uksort() should be used when sorting an associative array by key. Use ksort if your keys are simple numbers or text. Define a comparison function and use uksort() if your keys are more complicated objects such as arrays. Reverse User Sorts The functions sort(), asort(),and ksort() all have a matching reverse sort with an “r” in the function name.The user-defined sorts do not have reverse variants, but you 05 525x ch03 1/24/03 2:56 PM Page 83 84 Chapter 3 Using Arrays can sort a multidimensional array into reverse order.You provide the comparison func- tion, so write a comparison function that returns the opposite values.To sort into reverse order, the function will need to return 1 if x is less than y and –1 if x is greater than y. For example, function reverseCompare($x, $y) { if ( $x[2] == $y[2] ) return 0; else if ( $x[2] < $y[2] ) return 1; else return -1; } Calling usort($products, reverseCompare) would now result in the array being placed in descending order by price. Reordering Arrays For some applications, you might want to manipulate the order of the array in other ways.The function shuffle() randomly reorders the elements of your array.The func- tion array_reverse() gives you a copy of your array with all the elements in reverse order. Using shuffle() Bob wants to feature a small number of his products on the front page of his site. He has a large number of products, but would like three randomly selected items shown on the front page. So that repeat visitors do not get bored, he would like the three chosen prod- ucts to be different for each visit. He can easily accomplish his goal if all his products are in an array. Listing 3.1 displays three randomly chosen pictures by shuffling the array into a random order and then displaying the first three. Listing 3.1 bobs_front_page.php—Using PHP to Produce a Dynamic Front Page for Bob’s Auto Parts <?php $pictures = array('tire.jpg', 'oil.jpg', 'spark_plug.jpg', 'door.jpg', 'steering_wheel.jpg', 'thermostat.jpg', 'wiper_blade.jpg', 'gasket.jpg', 'brake_pad.jpg'); srand ((float)microtime()*1000000); shuffle($pictures); ?> <html> <head> 05 525x ch03 1/24/03 2:56 PM Page 84 85 Reordering Arrays <title>Bob's Auto Parts</title> </head> <body> <center> <h1>Bob's Auto Parts</h1> <table width = 100%> <tr> <?php for ( $i = 0; $i < 3; $i++ ) { echo '<td align="center"><img src="'; echo $pictures[$i]; echo '"width="100" height="100"></td>'; } ?> </tr> </table> </center> </body> </html> Because the code selects random pictures, it produces a different page nearly every time you load it, as shown in Figure 3.5. Listing 3.1 Continued Figure 3.5 The shuffle() function enables us to feature three randomly chosen products. 05 525x ch03 1/24/03 2:56 PM Page 85 86 Chapter 3 Using Arrays Any of the random number functions require that you seed the random number genera- tor first by calling srand().You will see that we did this in Listing 3.1. The shuffle() function has not had a very illustrious history. In older versions of PHP it does not shuffle very well, giving a result that is not very random. In version 4.2.x on Windows, it does not shuffle at all, giving a result that was exactly what you started with. If it is important to you, test it on your server. Using array_reverse() The function array_reverse() takes an array and creates a new one with the same contents in reverse order. For example, there are a number of ways to create an array containing a countdown from ten to one. Because using range() alone creates an ascending sequence, we must then use rsort() to sort the numbers into descending order.Alternatively, we could create the array one element at a time by writing a for loop: $numbers = array(); for($i=10; $i>0; $i ) array_push( $numbers, $i ); A for() loop can go in descending order like this.We set the starting value high, and at the end of each loop use the operator to decrease the counter by one. We created an empty array, and then used array_push() for each element to add one new element to the end of an array. As a side note, the opposite of array_push() is array_pop().This function removes and returns one element from the end of an array. Alternatively, we can use the array_reverse() function to reverse the array created by range(). $numbers = range(1,10); $numbers = array_reverse($numbers); Note that array_reverse() returns a modified copy of the array. Because we did not want the original array, we simply stored the new copy over the original. Loading Arrays from Files In Chapter 2,“Storing and Retrieving Data,” we stored customer orders in a file. Each line in the file looks something like: 15:42, 20th April 4 tires 1 oil 6 spark plugs $434.00 22 Short St, Smalltown To process or fulfill this order, we could load it back into an array. Listing 3.2 displays the current order file. 05 525x ch03 1/24/03 2:56 PM Page 86 . Listing 3.1 displays three randomly chosen pictures by shuffling the array into a random order and then displaying the first three. Listing 3.1 bobs_front_page .php Using PHP to Produce a Dynamic. “user” because this function requires a user-defined comparison function.The uasort() and uksort() versions of asort and ksort also require a user-defined comparison function. Similar to asort(),. products sort- ed into alphabetical order using the description or by numeric order by the price. Either result is possible, but we need to use the function usort() and tell PHP how to com- pare the