This next piece of code is designed to create a multilevel array, print it, then shuffle it, print it again, and sort the array.
$arr=array(
array(
array('a','b','c')
),
array(
array('d','e','f')
),
array(
array('g','h','i')
),
);
print_r($arr);
shuffle($arr);
print_r($arr);
sort($arr);
print_r($arr);
Now, what I noticed when used shuffle(), it only shuffles the indexes of the array, which is shuffled, it does not shuffle, the majority of the elements are a,b,csomething else, but when the function is used sort(), it sorts the array in the normal state and the leaf nodes are returned in alphabetical order . Why is this happening?
Here is an example output:
* Original array *
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
Jumbled array
Array
(
[0] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
[1] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[2] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
)
Sorted array
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)