PHP array_reduce() Function


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

<?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

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

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


Complete PHP Array Reference
Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!