PHP

PHP reverse sort and random sort code


Introduction to the array_reverse() and shuffle() functions

**Array_reverse () **The array array_reverse() function passes in the parameters as an array and returns an array with the same values but in the opposite order.


<?php
$a = array(1,2,3,4,5);
$a = array_reverse($a);
for ($i=0; $i<count($a); ++$i)
echo $a[$i]." ";
?>

The result is:

5, 4, 3, 2, 1

The shuffle () The bool shuffle(array)shuffle function randomly sorts the incoming array, returning TRUE on success or FALSE otherwise.


<?php
$a = array(1,2,3,4,5);
shuffle($a);
for ($i=0; $i<count($a); ++$i)
echo $a[$i]." ";
shuffle($a);
echo "<br />";
for ($i=0; $i<count($a); ++$i)
echo $a[$i]." ";
?>

Results returned from two calls:

4, 1, 2, 5, 3 1, 5, 2, 4, 3