Learning PHP
Advanced PHP
PHP Function Reference
PHP Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM
|
PHP Function array_reduce()
Syntax
array_reduce ( $array, callback $function [, int $initial] );
|
Definition and Usage
This function applies iteratively the function function to the elements of the array , so as to reduce the array to a single value. If the optional initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty. If the array is empty and initial is not passed.
Paramters
Parameter | Description |
array | Required. Specifies an array. |
function | Required. Callback function. |
initial | Optional. Specifies the initial value to send to the function. |
Return Values
Returns a reduced array.
Example
Try out following example:
<?php
function call_back_function($v1,$v2)
{
return $v1 . "-" . $v2;
}
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");
print_r(array_reduce($array));
print_r("<br />");
print_r(array_reduce($array, 10));
?>
|
This will produce following result:
-banana-apple-orange
10-banana-apple-orange
|
|
|
|