array_reduce() Function in PHP
What is array_reduce() Function?
Explanation
The "array_reduce()" function is used to send the values in an array to a user defined function and return a string.
Syntax:
array_reduce(array,function,initial value)
In the above syntax "array" specifies the array from which the values are passed to the function, even one can specify the initial value to be passed.
Example :
<?php
function funct1($x1,$x2)
{
return $x1 . "-" . $x2;
}
function funct2 ($y1,$y2)
{
return $y1+$y2
}
$a=array("Orange","Guava","Apple");
$b=array(5,10,20);
print_r(array_reduce($a,"funct1",6));
print "<br>";
print_r(array_reduce($b,"funct2",6));
?>
Result :
6-Orange-Guava-Apple
41
In the above example "funct1" returns a string with the intial value 6 added to the existing array, in the second "funct2" it returns the sum of the numbers of the array "$b".