array_reduce
Iteratively simplify the array to a single value with a callback function
array_reduce()
function sends the value in the array to the user-defined function and returns a string.
Note: If the array is empty and no initial parameter is passed, the function returns NULL.
Send values in the array to the user-defined function and return a string:
<?php function myfunction ( $v1 , $v2 ) { return $v1 . "-" . $v2 ; } $a = array ( "Dog" , "Cat" , "Horse" ) ; print_r ( array_reduce ( $a , "myfunction" ) ) ; ?>
Try it yourself
Set initial parameters:
<?php function myfunction ( $v1 , $v2 ) { return $v1 . "-" . $v2 ; } $a = array ( "Dog" , "Cat" , "Horse" ) ; print_r ( array_reduce ( $a , "myfunction" , 5 ) ) ; ?>
Try it yourself
Returns the sum:
<?php function myfunction ( $v1 , $v2 ) { return $v1 + $v2 ; } $a = array ( 10 , 15 , 20 ) ; print_r ( array_reduce ( $a , "myfunction" , 5 ) ) ; ?>
Try it yourself