Collection::shuffle PHP Method

shuffle() public method

Shuffle all elements in the array
public shuffle ( ) : object
return object a new shuffled collection
    public function shuffle()
    {
        $keys = array_keys($this->data);
        shuffle($keys);
        $collection = clone $this;
        $collection->data = array();
        foreach ($keys as $key) {
            $collection->data[$key] = $this->data[$key];
        }
        return $collection;
    }

Usage Example

 public function testShuffleReindexed()
 {
     $collection = new Collection(['first' => new Bar('a'), 'second' => new Bar('b'), 'third' => new Bar('c'), 'fourth' => new Bar('c')]);
     $result = $collection->shuffle(false);
     $this->assertArrayHasKey(0, $result);
     $this->assertArrayHasKey(1, $result);
     $this->assertArrayHasKey(2, $result);
     $this->assertArrayHasKey(3, $result);
 }