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 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:
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:
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:
Complete PHP Array Reference
|
|
|
See why there are 20,000+ Ektron integrations worldwide.
Request an INSTANT DEMO or download a FREE TRIAL today. |
|
|
|