From http://www.w3schools.com (Copyright Refsnes Data)

PHP array_reduce() Function


PHP Array Reference Complete PHP Array Reference

Definition and Usage

The array_reduce() function sends the values in an array to a user-defined function, and returns a string.

Syntax

array_reduce(array,function,initial)

Parameter Description
array Required. Specifies an array
function Required. Specifies the name of the function
initial Optional. Specifies the initial value to send to the function


Example 1

<?php
function myfunction($v1,$v2) 
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction"));
?>

The output of the code above will be:

-Dog-Cat-Horse


Example 2

With the initial parameter:

<?php
function myfunction($v1,$v2) 
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction",5));
?>

The output of the code above will be:

5-Dog-Cat-Horse


Example 3

Returning a sum:

<?php
function myfunction($v1,$v2) 
{
return $v1+$v2;
}
$a=array(10,15,20);
print_r(array_reduce($a,"myfunction",5));
?>

The output of the code above will be:

50


PHP Array Reference Complete PHP Array Reference

From http://www.w3schools.com (Copyright Refsnes Data)