lithium\util\Collection::reduce PHP Method

reduce() public method

Reduce, or fold, a collection down to a single value
public reduce ( callback $reducer, mixed $initial = false ) : mixed
$reducer callback The reduce function, i.e. `function($carry, $item) { return ... }`
$initial mixed Initial value passed to the reduce function as `$carry`, defaults to `false`.
return mixed A single reduced value.
    public function reduce($reducer, $initial = false)
    {
        return array_reduce($this->_data, $reducer, $initial);
    }

Usage Example

Example #1
0
 public function testCollectionReduceFilter()
 {
     $collection = new Collection(array('data' => array(1, 2, 3)));
     $filter = function ($memo, $item) {
         return $memo + $item;
     };
     $result = $collection->reduce($filter, 0);
     $this->assertEqual(6, $collection->reduce($filter, 0));
     $this->assertEqual(7, $collection->reduce($filter, 1));
 }
All Usage Examples Of lithium\util\Collection::reduce