FOF30\Utils\Collection::random PHP Method

random() public method

Get one or more items randomly from the collection.
public random ( integer $amount = 1 ) : mixed
$amount integer
return mixed
    public function random($amount = 1)
    {
        $keys = array_rand($this->items, $amount);
        return is_array($keys) ? array_intersect_key($this->items, array_flip($keys)) : $this->items[$keys];
    }

Usage Example

Esempio n. 1
0
 public function testRandom()
 {
     $data = new Collection(array(1, 2, 3, 4, 5, 6));
     $random = $data->random();
     $this->assertInternalType('integer', $random);
     $this->assertContains($random, $data->all());
     $random = $data->random(3);
     $this->assertCount(3, $random);
 }