A função Ds \ Map :: reduce () pode reduzir o mapa a um único valor usando uma função de retorno de chamada.
Sintaxe
public mixed Ds\Map::reduce( callable $callback [, mixed $initial ] )
A função Ds \ Map :: reduce () pode retornar um valor de retorno de chamada final.
Exemplo 1
<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
echo("The map elements: \n");
print_r($map);
echo("\n An element after performing operation: \n");
var_dump($map->reduce(function($carry, $key, $element) {
return $carry + $element + 2;
}));
?>
Exemplo 2
<?php
$map = new \Ds\Map(["a" => 10, "b" => 20, "c" => 30, "d" => 40]);
echo("The original map elements: \n");
print_r($map);
$func = function($carry, $key, $element) {
return $carry * $element;
};
echo("\n The map after reducing to single element: \n");
var_dump($map->reduce($func, 10));
?>