PHP shuffle() not randomizing an array the way I need it. I have a two-dimensional array, and when I use shuffle() , it only randomizes the 2-dimensional dimension of the array, but I need the opposite.
Suppose this is an array that I need to shuffle:
Array ( [0] => Array ( [key1] => 199 [key2] => 6 ) [1] => Array ( [key1] => 195 [key2] => 3 ) )
By shuffling shuffle() it looks like this:
Array ( [0] => Array ( [key1] => 195 [key2] => 3 ) [1] => Array ( [key1] => 199 [key2] => 6 ) )
But that is not what I need. Ultimately, I need the following:
Array ( [1] => Array ( [key1] => 195 [key2] => 6 ) [0] => Array ( [key1] => 199 [key2] => 6 ) )
I know that this can be achieved using a random key with rand() or mt_rand() , but it is also possible that for a small number of keys we could get the same rand() key twice, NOT have a beautifully shuffled array.
I also know that adding if else logic would be possible, but I want to do this with material already implemented - I don't want to reinvent the wheel.
How can I achieve the desired shuffle?
Aborted
source share