src/Categories/Getters.php
public static function selectRandomArrayElements(array $array, $numberOfRequiredElements)
    {
        // validation, must be positive int or 0
        if (!self::isLogicallyCastableToInt($numberOfRequiredElements)) {
            throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer');
        }
        if ($numberOfRequiredElements <= 0) {
            throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer');
        }

        $selected = $array;
        if (count($array) > $numberOfRequiredElements) {
            // select required number of random keys
            $selectedKeys = array_rand($array, $numberOfRequiredElements);
            $selectedKeys = $numberOfRequiredElements == 1 ? [$selectedKeys] : $selectedKeys;
            // select only array members with selected random keys
            $selected = array_intersect_key($array, array_fill_keys($selectedKeys, null));
        }

        return $selected;
    }
public static function selectRandomArrayElements(array $array, $numberOfRequiredElements)
    {
        // validation, must be positive int or 0
        if (!self::isLogicallyCastableToInt($numberOfRequiredElements)) {
            throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer');
        }
        if ($numberOfRequiredElements <= 0) {
            throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer');
        }

        $selected = $array;
        if (count($array) > $numberOfRequiredElements) {
            // select required number of random keys
            $selectedKeys = array_rand($array, $numberOfRequiredElements);
            $selectedKeys = $numberOfRequiredElements == 1 ? [$selectedKeys] : $selectedKeys;
            // select only array members with selected random keys
            $selected = array_intersect_key($array, array_fill_keys($selectedKeys, null));
        }

        return $selected;
    }
public static function selectRandomArrayElements ( array $ array , $ numberOfRequiredElements ) { // validation, must be positive int or 0 if ( ! self :: isLogicallyCastableToInt ( $ numberOfRequiredElements ) ) { throw new InvalidArgumentException ( 'Number of requested elements parameter must be a positive integer' ) ; } if ( $ numberOfRequiredElements <= 0 ) { throw new InvalidArgumentException ( 'Number of requested elements parameter must be a positive integer' ) ; } $ selected = $ array ; if ( count ( $ array ) > $ numberOfRequiredElements ) { // select required number of random keys $ selectedKeys = array_rand ( $ array , $ numberOfRequiredElements ) ; $ selectedKeys = $ numberOfRequiredElements == 1 ? [ $ selectedKeys ] : $ selectedKeys ; // select only array members with selected random keys $ selected = array_intersect_key ( $ array , array_fill_keys ( $ selectedKeys , null ) ) ; } return $ selected ; }
Selects random sub array

@param array $array
@param int $numberOfRequiredElements

@return array
@throws InvalidArgumentException
Selects random sub array