Share on Google+Share on Google+

PHP Array Random Sorting

Advertisement
PHP Array Random Sorting, PHP provides a special function shuffle, which can be use to shuffle the elements of an array.

PHP Array Random Sort

PHP provides another kind of sorting which is called Random Sorting,  in PHP shuffle() is used to sort in this manner. This function shuffles an array.

Small description of shuffle function is as follows:

General Format Boolean shuffle(array $array)
Parameters array: the array to be sorted
Return Value True/False

Example 1:

<?php

$array=array(11,3,85);

shuffle($array);

while($val=each($array))

{

echo $val['key'];

echo '=>';

echo $val['value'].'<br/>';

}

?>

Output (The output will keep changing after pressing refresh/F5 button):

First Instance:

0=>85
1=>3
2=>11

Second Instance:

0=>85
1=>11
2=>3

Third Instance:

0=>11
1=>85
2=>3

And so on...

Advertisements

Advertisement

Posted on: October 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.