PHP Tutorial array element Random sort code
Because the job requires me to randomly sort the array I've defined, and each time the loop is different, we use the PHP shuffle function to sort by random array elements. method is very simple.
The shuffle () function rearranges the elements in the array in random order.
If successful, returns TRUE, otherwise returns false.
Note: This function assigns a new key name to the cell in the array. This deletes the original key name and not just the reordering.
*/
$array = Array (1,2,3,4);
For ($i =1 $i <30; $i + +)
{
Shuffle ($array);
Print_r ($array);
echo ' <br/> ';
}
/*
Array ([0] => 3 [1] => 2 [2] => 4 [3] => 1)
Array ([0] => 3 [1] => 4 [2] => 1 [3] => 2)
Array ([0] => 2 [1] => 1 [2] => 3 [3] => 4)
Array ([0] => 2 [1] => 4 [2] => 1 [3] => 3)
Array ([0] => 3 [1] => 4 [2] => 2 [3] => 1)
Array ([0] => 3 [1] => 2 [2] => 4 [3] => 1)
Array ([0] => 3 [1] => 4 [2] => 1 [3] => 2)
Array ([0] => 4 [1] => 2 [2] => 1 [3] => 3)
Array ([0] => 2 [1] => 4 [2] => 1 [3] => 3)
Array ([0] => 4 [1] => 3 [2] => 1 [3] => 2)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 4 [1] => 3 [2] => 1 [3] => 2)
Array ([0] => 4 [1] => 1 [2] => 2 [3] => 3)
Array ([0] => 2 [1] => 1 [2] => 4 [3] => 3)
Array ([0] => 3 [1] => 1 [2] => 4 [3] => 2)
Array ([0] => 2 [1] => 1 [2] => 3 [3] => 4)
Array ([0] => 4 [1] => 1 [2] => 3 [3] => 2)
Array ([0] => 1 [1] => 4 [2] => 2 [3] => 3)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 1 [1] => 2 [2] => 4 [3] => 3)
Array ([0] => 1 [1] => 3 [2] => 4 [3] => 2)
Array ([0] => 2 [1] => 4 [2] => 3 [3] => 1)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 1 [1] => 3 [2] => 4 [3] => 2)
Array ([0] => 2 [1] => 1 [2] => 4 [3] => 3)
Array ([0] => 4 [1] => 1 [2] => 3 [3] => 2)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)
Array ([0] => 2 [1] => 1 [2] => 4 [3] => 3)
Summarize:
PHP has a lot of functions, and if you know that this function will help you develop a lot, it may take a lot of time if you don't know what to write yourself.
*/