Send the values in the array to a user-defined function and return a string:
<?phpfunction myfunction($v1,$v2){return $v1 . "-" . $v2;}$a=array("Dog","Cat","Horse");print_r(array_reduce($a," myfunction"));?>The array_reduce() function sends the values in the array to a user-defined function and returns a string.
Note: If the array is empty or no initial value is passed, this function returns NULL.
array_reduce( array,myfunction,initial )
parameter | describe |
---|---|
array | Required. Specifies an array. |
myfunction | Required. Specifies the name of the function. |
initial | Optional. Specifies the first value sent to the function for processing. |
Return value: | Return the result value. |
---|---|
PHP version: | 4.0.5+ |
Update log: | Since PHP 5.3.0, the initial parameter accepts multiple types (mixed), and versions before PHP 5.3.0 only support integers. |
With initial parameters:
<?phpfunction myfunction($v1,$v2){return $v1 . "-" . $v2;}$a=array("Dog","Cat","Horse");print_r(array_reduce($a," myfunction",5));?>Return the sum:
<?phpfunction myfunction($v1,$v2){return $v1+$v2;}$a=array(10,15,20);print_r(array_reduce($a,"myfunction",5));?>