HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
PHP Array Reference : Send the values in an array to a user-defined function and return a string
The array_reduce() function sends the values in an array to a user-defined function, and returns a string.
Parameter | Description |
---|---|
array | Required. Specifies an array |
myfunction | Required. Specifies the name of the function |
initial | Optional. Specifies the initial value to send to the function |
Return Value: | Returns the resulting value |
---|---|
PHP Version: | 4.0.5+ |
PHP Changelog: | As of PHP 5.3.0, the initial parameter accepts multiple types (mixed). Versions prior to PHP 5.3.0, only allowed integer. |
With the initial parameter:
<?php
function myfunction($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction",5));
?>
Returning a sum:
<?php
function myfunction($v1,$v2)
{
return $v1+$v2;
}
$a=array(10,15,20);
print_r(array_reduce($a,"myfunction",5));
?>
❮ PHP Array Reference