PHP sends the values ​​in the array to the user-defined function and returns a string function array_reduce()

黄舟
Release: 2023-03-17 08:22:01
Original
1587 people have browsed it

Example

Send the values ​​in the array to the userCustom function, and return a string:

<?php
function myfunction($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction"));
?>
Copy after login

Definition and usage

array_reduce () function sends the values ​​in the array to the user-defined function and returns a string.

Note: If the array is empty or the initial value is not passed, this function returns NULL.

Syntax

array_reduce(array,myfunction,initial)
Copy after login
ParametersDescription
array Required. Specifies an array.
myfunctionRequired. Specifies the name of the function.
initialOptional. Specifies the first value sent to the function for processing.

Technical details

Return value: Return the result value.
PHP Version: 4.0.5+
Change Log:Since PHP 5.3.0, the initial parameter accepts multiple types (mixed), and versions before PHP 5.3.0 only support integers.

更多实例

实例 1

带 initial 参数:

<?php
function myfunction($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a,"myfunction",5));
?>
Copy after login

实例 2

返回总和:

<?php
function myfunction($v1,$v2)
{
return $v1+$v2;
}
$a=array(10,15,20);
print_r(array_reduce($a,"myfunction",5));
?>
Copy after login

array_reduce的强大不仅如此。看下面的例子。将数组$arr的首个元素弹出,作为初始值,避免min($result['min'], $item['min'])中$result为空。

否则最终结果min是空的。

$arr = array( 
    array(&#39;min&#39; => 1.5456, &#39;max&#39; => 2.28548, &#39;volume&#39; => 23.152), 
    array(&#39;min&#39; => 1.5457, &#39;max&#39; => 2.28549, &#39;volume&#39; => 23.152), 
    array(&#39;min&#39; => 1.5458, &#39;max&#39; => 2.28550, &#39;volume&#39; => 23.152), 
    array(&#39;min&#39; => 1.5459, &#39;max&#39; => 2.28551, &#39;volume&#39; => 23.152), 
    array(&#39;min&#39; => 1.5460, &#39;max&#39; => 2.28552, &#39;volume&#39; => 23.152), 
); 

$initial = array_shift($arr); 

$t = array_reduce($arr, function($result, $item) { 
    $result[&#39;min&#39;] = min($result[&#39;min&#39;], $item[&#39;min&#39;]); 
    $result[&#39;max&#39;] = max($result[&#39;max&#39;], $item[&#39;max&#39;]); 
    $result[&#39;volume&#39;] += $item[&#39;volume&#39;]; 

    return $result; 
}, $initial);
Copy after login

 总之,这种写法比foreach更优雅,更少的定义变量。推荐使用。


The above is the detailed content of PHP sends the values ​​in the array to the user-defined function and returns a string function array_reduce(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template