PHP array_reduce() 函数

PHP 教程 · 2019-01-22 12:29:27

实例

发送数组中的值到用户自定义函数,并返回一个字符串:

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

定义和用法

array_reduce() 函数发送数组中的值到用户自定义函数,并返回一个字符串。

注释:如果数组是空的或者初始值未传递,该函数返回 NULL。

语法

array_reduce(array,myfunction,initial)

参数 描述
array 必需。规定数组。
myfunction 必需。规定函数的名称。
initial 可选。规定发送到函数处理的第一个值。

技术细节

返回值: 返回结果值。
PHP 版本: 4.0.5+
更新日志: 自 PHP 5.3.0 起,initial 参数接受多类型(混合的),PHP 5.3.0 之前的版本只支持整数。

更多实例

实例 1

带 initial 参数:

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

实例 2

返回总和:

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

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

500 Lines or Less

500 Lines or Less

Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00

This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具