Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

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.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');

 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.5Figure 3.5 The shuffle() function enables you to feature three randomly chosen products.

In older versions of PHP, the shuffle() function required that you seed the random number generator first by calling srand(). This step is no longer required.

The shuffle() function has not had a very illustrious history. In older versions of PHP, it did not shuffle very well, giving a result that was not very random. In version 4.2.x on Windows, for instance, it did not shuffle at all, giving a result that was exactly what you started with. In version 5, it seems to work. If this function is important to you, test it on your server before employing it in your applications.

Because you do not really need the whole array reordered, you can achieve the same result using the function array_rand().

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 10 to 1.

Using range() usually creates an ascending sequence, which you could place in descending order using array_reverse() or rsort(). Alternatively, you 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: You set the starting value high and at the end of each loop use the -- operator to decrease the counter by one.

Here, you create an empty array and then use 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, you 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. If you do not want the original array, as in this example, you can simply store the new copy over the original.

If your data is just a range of integers, you can create it in reverse order by passing –1 as the optional step parameter to range():

$numbers = range(10, 1, -1);
  • + Share This
  • 🔖 Save To Your Account