Ds\Map::reduce

(PECL ds >= 1.0.0)

Ds\Map::reduceReduces the map to a single value using a callback function

说明

public Ds\Map::reduce ( callable $callback [, mixed $initial ] ) : mixed

Reduces the map to a single value using a callback function.

参数

callback
callback ( mixed $carry , mixed $key , mixed $value ) : mixed
carry

The return value of the previous callback, or initial if it's the first iteration.

key

The key of the current iteration.

value

The value of the current iteration.

initial

The initial value of the carry value. Can be NULL.

返回值

The return value of the final callback.

范例

Example #1 Ds\Map::reduce() with initial value example

<?php
$map 
= new \Ds\Map(["a" => 1"b" => 2"c" => 3]);

$callback = function($carry$key$value) {
    return 
$carry $value;
};

var_dump($map->reduce($callback5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>

以上例程的输出类似于:

int(30)

Example #2 Ds\Map::reduce() without an initial value example

<?php
$map 
= new \Ds\Map(["a" => 1"b" => 2"c" => 3]);

var_dump($map->reduce(function($carry$key$value) {
    return 
$carry $value 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>

以上例程的输出类似于:

int(21)
相关文章
php callbackfilteriterator calls the callback with the current value the current key and the inner iterator as argumentsphp deque updates all values by applying a callback function to each valuephp deque reduces the deque to a single value using a callback functionphp map updates all values by applying a callback function to each valuephp map returns the result of applying a callback to each valuephp map reduces the map to a single value using a callback functionphp map creates a new map using values from the current instance and another mapphp sequence updates all values by applying a callback function to each valuephp sequence reduces the sequence to a single value using a callback functionphp set reduces the set to a single value using a callback functionphp vector updates all values by applying a callback function to each valuephp vector reduces the vector to a single value using a callback functionphp fdf 函数 call a user defined function for each document valuephp 多字节字符串 函数 perform a regular expression search and replace with multibyte support using a callbackphp oci8 函数 unregister a user defined callback function for oracle database tafphp swoole 函数 check if a timer callback function is existedphp swoole 函数 trigger a timer tick callback function by time intervalphp gearmanworker register and add callback functionphp solrcollapsefunction selects the group heads by the max value of a numeric field or function queryphp swoole event add a callback function to the next event loop.
关注编程学问公众号