$value..."/>

Shuffle Function in Array

This function randomizes the order of the elements in an array also used together with srand() function.

shuffle() example:

$ar_value = range('A','F'); // create and fill the elements of $sar_value = array('A','B',...'F');
echo "First value:
\n”;
WHILE(list($key,$value) = each($ar_value)){
echo “Index[ {$key} ] => $value
\n”;
}
echo “
\n”;
echo “After shuffle():
\n”;
srand((float)microtime()*1000000);
shuffle($ar_value);
WHILE(list($key,$value) = each($ar_value)){
echo “Index[ {$key} ] => $value
\n”;
}

And the result of the code above:

First value:
Index[ 0 ] => A
Index[ 1 ] => B
Index[ 2 ] => C
Index[ 3 ] => D
Index[ 4 ] => E
Index[ 5 ] => F

After shuffle():
Index[ 0 ] => C
Index[ 1 ] => B
Index[ 2 ] => A
Index[ 3 ] => F
Index[ 4 ] => D
Index[ 5 ] => E

Related Posts:

  • Multiple (Multi-dimensional) Array in NuSoap
    NuSoap - WSDL untuk data array, bagaimana caranya? mengirim variabel output array kepada aplikasi client. syntax xsd:array tidak ada. Ini contoh aplikasi sederhana untuk menampilkan data mahasiswa dar...

Tags: , ,

20.Oct.08 PHP


You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Leave a Comment

:)