src/Map.php
protected function grep($expression, $limit = null)
    {
        // initialize our return map and book-keeping values
        $map = new static;
        $bnd = empty($limit) ? null : abs($limit);
        $cnt = 0;

        // define a helper to add matching values to our new map, stopping when
        // any designated limit is reached
        $helper = function ($value, $key) use ($expression, $map, $bnd, &$cnt) {
            if ($this->passes($this->call($expression, $value, $key))) {
                $map->set($key, $value);
                if (null !== $bnd && $bnd <= ++$cnt) {
                    return false;
                }
            }
        };

        // walk the array in the right direction
        if (0 <= $limit) {
            $this->walk($helper);

        } else {
            $this->walk_backward($helper);
        }

        return $map;
    }
protected function grep($expression, $limit = null)
    {
        // initialize our return map and book-keeping values
        $map = new static;
        $bnd = empty($limit) ? null : abs($limit);
        $cnt = 0;

        // define a helper to add matching values to our new map, stopping when
        // any designated limit is reached
        $helper = function ($value, $key) use ($expression, $map, $bnd, &$cnt) {
            if ($this->passes($this->call($expression, $value, $key))) {
                $map->set($key, $value);
                if (null !== $bnd && $bnd <= ++$cnt) {
                    return false;
                }
            }
        };

        // walk the array in the right direction
        if (0 <= $limit) {
            $this->walk($helper);

        } else {
            $this->walk_backward($helper);
        }

        return $map;
    }
protected function grep ( $ expression , $ limit = null ) { // initialize our return map and book-keeping values $ map = new static ; $ bnd = empty ( $ limit ) ? null : abs ( $ limit ) ; $ cnt = 0 ; // define a helper to add matching values to our new map, stopping when // any designated limit is reached $ helper = function ( $ value , $ key ) use ( $ expression , $ map , $ bnd , & $ cnt ) { if ( $ this -> passes ( $ this -> call ( $ expression , $ value , $ key ) ) ) { $ map -> set ( $ key , $ value ) ; if ( null !== $ bnd && $ bnd <= ++ $ cnt ) { return false ; } } } ; // walk the array in the right direction if ( 0 <= $ limit ) { $ this -> walk ( $ helper ) ; } else { $ this -> walk_backward ( $ helper ) ; } return $ map ; }
Finds elements for which the given code passes, optionally limited to a
maximum count.

If limit is null, no limit on number of matches. If limit is positive,
return that many from the front of the array. If limit is negative,
return that many from the end of the array.

@param callable|string $expression
@param int|null $limit
@return new static
Finds elements for which the given code passes optionally limited to a maximum count .