src/Collection.php
final public function filter(callable $filter, array $args = []): self
    {
        // Start a new Collection to collect the filtered results
        $collection = new Collection();

        // Iterate over current Collection
        iterator_apply(

            // Use $this object as the iterator
            $this,

            // Wrap callable in closure that alway returns true
            function(IteratorAggregate $iterator) use ($filter, $args, &$collection) {

                // Pass current element and optional arguments to callable
                if ($filter($iterator->current(), $args) === true) {

                    $collection->set(
                        $iterator->key(),
                        $iterator->current()
                    );
                }

                return true;
            },

            // Pass $this (again) to the function for reasons unbeknown to science
            // {@see https://www.reddit.com/r/lolphp/comments/5zkn29/what_the_hell_with_iterator_apply/}
            [$this->toArray()]
        );

        // Return filtered Collection
        return $collection;
    }
final public function filter(callable $filter, array $args = []): self
    {
        // Start a new Collection to collect the filtered results
        $collection = new Collection();

        // Iterate over current Collection
        iterator_apply(

            // Use $this object as the iterator
            $this,

            // Wrap callable in closure that alway returns true
            function(IteratorAggregate $iterator) use ($filter, $args, &$collection) {

                // Pass current element and optional arguments to callable
                if ($filter($iterator->current(), $args) === true) {

                    $collection->set(
                        $iterator->key(),
                        $iterator->current()
                    );
                }

                return true;
            },

            // Pass $this (again) to the function for reasons unbeknown to science
            // {@see https://www.reddit.com/r/lolphp/comments/5zkn29/what_the_hell_with_iterator_apply/}
            [$this->toArray()]
        );

        // Return filtered Collection
        return $collection;
    }
final public function filter ( callable $ filter , array $ args = [ ] ) : self { // Start a new Collection to collect the filtered results $ collection = new Collection ( ) ; // Iterate over current Collection iterator_apply ( // Use $this object as the iterator $ this , // Wrap callable in closure that alway returns true function ( IteratorAggregate $ iterator ) use ( $ filter , $ args , & $ collection ) { // Pass current element and optional arguments to callable if ( $ filter ( $ iterator -> current ( ) , $ args ) === true ) { $ collection -> set ( $ iterator -> key ( ) , $ iterator -> current ( ) ) ; } return true ; } , // Pass $this (again) to the function for reasons unbeknown to science // {@see https://www.reddit.com/r/lolphp/comments/5zkn29/what_the_hell_with_iterator_apply/} [ $ this -> toArray ( ) ] ) ; // Return filtered Collection return $ collection ; }
Filter the current collection by a user-defined function

@param callable $filter - the user-defined function to filter by
@param array $args - OPTIONAL array of arguments to pass to the callable
@return self
Filter the current collection by a user - defined function