Neos\Eel\Helper\ArrayHelper::shuffle PHP Method

shuffle() public method

Randomizes entries an array with the option to preserve the existing keys. When this option is set to FALSE, all keys will be replaced
public shuffle ( array $array, boolean $preserveKeys = true ) : array
$array array
$preserveKeys boolean Wether to preserve the keys when shuffling the array
return array The shuffled array
    public function shuffle(array $array, $preserveKeys = true)
    {
        if ($array === []) {
            return $array;
        }
        if ($preserveKeys) {
            $keys = array_keys($array);
            shuffle($keys);
            $shuffledArray = [];
            foreach ($keys as $key) {
                $shuffledArray[$key] = $array[$key];
            }
            $array = $shuffledArray;
        } else {
            shuffle($array);
        }
        return $array;
    }

Usage Example

 /**
  * @test
  * @dataProvider shuffleExamples
  */
 public function shuffleWorks($array)
 {
     $helper = new ArrayHelper();
     $shuffledArray = $helper->shuffle($array);
     $this->assertEquals($array, $shuffledArray);
 }