Home >>PHP Array Functions >PHP array_reduce() Function

PHP array_reduce() Function

PHP array_reduce() Function

PHP array_reduce() Function in PHP language is generally used in order to send the values in an array to a user-defined function and this function returns a string.

Please note that in case the array is found to be empty and initial is not passed then this function will return NULL.

Syntax:

array_reduce(array, myfunction, initial)

Parameter Values

Parameter Description
array This is generally used to specify an array and it is required.
myfunction This parameter is usually used to specify the name of the function and it is also required.
initial This parameter is basically used to specify the initial value just as to send to the function. However, this is optional.

Here, is an example of array_reduce() Function in PHP:

<html>
<body>
<?php
function myfun($v1,$v2)
{
return $v1 . "-" . $v2;
}
$g=array("php","t","point");
print_r(array_reduce($g,"myfun"));
?>
</body>
</html>
Output:
-php-t-point

Example 2:

<html>
<body>
<?php  
function add($z, $a)  
{  
$z += $a;  
return $z;  
}  
$arya = array(6,3,4,5,6,8);  
$z=array_reduce($arya,"add");  
echo $z;  
?>  
</body>
</html>
Output: 32

No Sidebar ads