array_reduceITERATIVELY REDUCE an ARRAY to a single value using a callback function.
The $callback function must have two variables: $carry and item.
The $carry makes the first iteration it instead holds the value of $initial.
The $item holds the value of the current iteration.
If the $initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty.
<?php
mix array_reduce ( arr $array , callable $callback [, mix $initial = NULL ] )
where,
$array = The input ARRAY
$callback = The callback function used
$initial = The variable used to beginning the process
?>
$array
The input ARRAY.
$callback
The callback function used to reduce the ARRAY.
$initial
The variable used to beginning the process.
<?php
mix callback ( mix $carry , mix $item )
where,
$carry = Holds the return value of the previous iteration
$item = Holds the value of the current iteration.
?>
EXERCISE
<?php
// FUNCTION TO OBTAIN THE product01 OF THE ARRAY
function product01($i, $j)
{
$k = bcmul($i, $j);
return $k;
}
$arrB1 = [ 1, 3, 5, 7, 11, 13 ];
$in01 = 1;
echo 'Initial: ' . $in01 . '<br><br>';
$arrBN1 = array_reduce($arrB1, 'product01', $in01);
echo 'After array_reduce:<br>';
echo '( ' . $in01 . ' * ' . $arrBN1 . ' ) = ' . bcmul($in01, $arrBN1);
echo '<br><br>';
$init01 = mt_rand(0,13);
echo 'Input ARRAY:<br>';
print_r($arrB1);
echo '<br><br>';
echo 'Initial: ' , $init01 . '<br><br>';
$arrBNr = array_reduce($arrB1, 'product01', $init01);
echo 'After array_reduce:<br>';
echo '( ' . $init01 . ' * ' . $arrBN1 . ' ) = ' . $arrBNr;
echo '<br><br>';
?>
EXERCISE
<?php
// FUNCTION TO OBTAIN THE SUM OF THE ARRAY ELEMENTS
function sum02($i, $j)
{
$l = bcadd($i, $j);
return $l;
}
echo 'An empty ARRAY:<br>';
$empty02 = [];
print_r($empty02);
echo '<br><br>Without initial value:<br>';
$arrC2r = array_reduce($empty02, 'sum02');
var_dump($arrC2r);
$init02 = 1000001;
// AFTER ARRAY_REDUCE
$arrC3r = array_reduce($empty02, 'sum02', $init02);
echo '<br><br>initial value: ' . $init02 . '<br>';
print_r($arrC3r);
?>
EXERCISE
<?php
$array = array('foo', 'foo', 'bar', 'qux', 'qux', 'quux');
echo 'The given $array:<br>';
var_dump($array);
echo "<br><br>Testing array_reduce() to integer:<br>";
function reduce_int($w, $v) { return $w + strlen($v); }
$initial = 42;
var_dump(array_reduce($array,
'reduce_int',
$initial),
$initial);
echo "<br><br>Testing array_reduce() to float:<br>";
function reduce_float($w, $v) { return $w + strlen($v) / 10; }
$initial = 4.2;
var_dump(array_reduce($array,
'reduce_float',
$initial),
$initial);
echo "<br><br>Testing array_reduce() to string:<br>";
function reduce_string($w, $v) { return $w . $v; }
$initial = 'quux';
var_dump(array_reduce($array,
'reduce_string',
$initial),
$initial);
echo "<br><br>Testing array_reduce() to array:<br>";
function reduce_array($w, $v) { $w[$v]++; return $w; }
$initial = array('foo' => 42, 'bar' => 17,
'qux' => -2, 'quux' => 0);
var_dump(array_reduce($array,
'reduce_array',
$initial),
$initial);
echo "<br><br>Testing array_reduce() to null:<br>";
function reduce_null($w, $v) { return $w . $v; }
$initial = null;
var_dump(array_reduce($array,
'reduce_null',
$initial),
$initial);
?>
EXERCISE
<?php
echo "Testing array_reduce() : variation.";
function oneArg($v) {
return $v;
}
function threeArgs($v, $w, $x) {
return $v + $w + $x;
}
$array = array(1);
echo "<br><br>Testing with a callback with too few parameters:<br>";
var_dump(array_reduce($array, "oneArg", 2));
echo "<br><br>Testing with a callback with too many parameters:<br>";
try {
var_dump(array_reduce($array, "threeArgs", 2));
} catch (Throwable $e) {
echo "Exception: " . $e->getMessage() . "<br>";
}
?>
EXERCISE
<?php
echo "Testing array_reduce() : variation - object callbacks.";
class A {
static function adder($a, $b) {return $a + $b;}
public function adder2($a, $b) {return $a + $b;}
}
$array = array(1);
echo "<br><br>Static method callback:<br>";
var_dump(array_reduce($array, array("A", "adder")));
echo "<br><br>Instance method callback:<br>";
var_dump(array_reduce($array, array(new A(), "adder2")));
?>