src/Collection.php
final public function apply(callable $function, array $args = []): self
    {
        iterator_apply(

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

            // Wrap callable in closure that alway returns true
            function(CollectionType $collection) use ($function, $args) {

                // Set current key to result of callable
                $collection->set(

                    // Current key
                    $collection->key(),

                    // User-defined callable
                    $function(

                        // Current value
                        $collection->current(),

                        // Optional user-defined arguments
                        $args
                    )
                );

                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 to allow method-chaining
        return $this;
    }
final public function apply(callable $function, array $args = []): self
    {
        iterator_apply(

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

            // Wrap callable in closure that alway returns true
            function(CollectionType $collection) use ($function, $args) {

                // Set current key to result of callable
                $collection->set(

                    // Current key
                    $collection->key(),

                    // User-defined callable
                    $function(

                        // Current value
                        $collection->current(),

                        // Optional user-defined arguments
                        $args
                    )
                );

                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 to allow method-chaining
        return $this;
    }
final public function apply ( callable $ function , array $ args = [ ] ) : self { iterator_apply ( // Use $this object as the iterator $ this , // Wrap callable in closure that alway returns true function ( CollectionType $ collection ) use ( $ function , $ args ) { // Set current key to result of callable $ collection -> set ( // Current key $ collection -> key ( ) , // User-defined callable $ function ( // Current value $ collection -> current ( ) , // Optional user-defined arguments $ args ) ) ; 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 to allow method-chaining return $ this ; }
Apply a callback to every element of the collection

This method applies the suplied $function callable to each item of the
collection. The callable MAY modify the items but it MUST return a value.
The method SHOULD return the same instance to allow method-chaining.

@param callable $function - the user-defined function to apply
@param array $args - OPTIONAL array of arguments to pass to the callable
@return self
Apply a callback to every element of the collection