public static function getRandomElementsFromArray(array $values, int $count = 0) :array {

        $valuesLength = count($values);
        if ($count > $valuesLength)
        {
            throw new \InvalidArgumentException('Count larger than array length.');
        }

        if (!$count)
        {
            $count = random_int(0, $valuesLength);
        }

        if ($count === 0)
        {
            return [];
        }

        return array_intersect_key(
            $values,
            array_flip(
                (array) array_rand($values, $count)
            )
        );
    }
public static function getRandomElementsFromArray(array $values, int $count = 0) :array {

        $valuesLength = count($values);
        if ($count > $valuesLength)
        {
            throw new \InvalidArgumentException('Count larger than array length.');
        }

        if (!$count)
        {
            $count = random_int(0, $valuesLength);
        }

        if ($count === 0)
        {
            return [];
        }

        return array_intersect_key(
            $values,
            array_flip(
                (array) array_rand($values, $count)
            )
        );
    }
public static function getRandomElementsFromArray ( array $ values , int $ count = 0 ) : array { $ valuesLength = count ( $ values ) ; if ( $ count > $ valuesLength ) { throw new \ InvalidArgumentException ( 'Count larger than array length.' ) ; } if ( ! $ count ) { $ count = random_int ( 0 , $ valuesLength ) ; } if ( $ count === 0 ) { return [ ] ; } return array_intersect_key ( $ values , array_flip ( ( array ) array_rand ( $ values , $ count ) ) ) ; }
Get random elements from input array

@param array $values The input array
@param int $count The number of random elements you want to get in your response. Leave out or set to 0 for random.

@return array
@throws \InvalidArgumentException
@throws \Exception
Get random elements from input array