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 function 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 products 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.phpUsing 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> <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.
Figure 3.5 The shuffle() function enables us to feature three randomly chosen products.
Any of the random number functions require that you seed the random number generator 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.