PHP内置函数array_reverse、array_reduce、call_user_func和call_user_func_array
来源:互联网 发布:程序最小公倍数算法 编辑:程序博客网 时间:2024/06/05 03:12
1、array_reverse()函数比较简单,倒置数组:
$pipes = [ 'Pipe1', 'Pipe2', 'Pipe3', 'Pipe4', 'Pipe5', 'Pipe6',];$pipes = array_reverse($pipes);var_dump($pipes);// outputarray(6) { [0] => string(5) "Pipe6" [1] => string(5) "Pipe5" [2] => string(5) "Pipe4" [3] => string(5) "Pipe3" [4] => string(5) "Pipe2" [5] => string(5) "Pipe1"}
2、array_reduce内置函数主要是用回调函数去迭代数组中每一个值,并且每一次回调得到的结果值作为下一次回调的初始值
,最后返回最终迭代的值:/** * @link http://php.net/manual/zh/function.array-reduce.php * @param int $v * @param int $w * * @return int */function rsum($v, $w){ $v += $w; return $v;}$a = [1, 2, 3, 4, 5];// 10为初始值$b = array_reduce($a, "rsum", 10);// 最后输出 (((((10 + 1) + 2) + 3) + 4) + 5) = 25echo $b . PHP_EOL;
3、call_user_func()是执行回调函数,并可输入参数作为回调函数的参数,看测试代码:
class TestCallUserFunc{ public function index($request) { echo $request . PHP_EOL; }} /** * @param $test */function testCallUserFunc($test){ echo $test . PHP_EOL;}// [$class, $method]call_user_func(['TestCallUserFunc', 'index'], 'pipes'); // 输出'pipes'// Closurecall_user_func(function ($passable) { echo $passable . PHP_EOL;}, 'pipes'); // 输出'pipes'// functioncall_user_func('testCallUserFunc' , 'pipes'); // 输出'pipes'
4、call_user_func_array与call_user_func基本一样,只不过传入的参数是数组:class TestCallUserFuncArray{ public function index($request) { echo $request . PHP_EOL; }}/** * @param $test */function testCallUserFuncArray($test){ echo $test . PHP_EOL;}// [$class, $method]call_user_func_array(['TestCallUserFuncArray', 'index'], ['pipes']); // 输出'pipes'// Closurecall_user_func_array(function ($passable) { echo $passable . PHP_EOL;}, ['pipes']); // 输出'pipes'// functioncall_user_func_array('testCallUserFuncArray' , ['pipes']); // 输出'pipes'
0 0
- PHP内置函数array_reverse、array_reduce、call_user_func和call_user_func_array
- call_user_func函数和call_user_func_array函数(PHP)
- PHP之call_user_func函数和call_user_func_array函数
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php自定义函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- php自定义函数call_user_func和call_user_func_array详解
- php函数call_user_func和call_user_func_array详解
- PHP 函数之 call_user_func & call_user_func_array
- call_user_func函数和call_user_func_array函数
- call_user_func函数和call_user_func_array函数
- 介词短语
- setDateAndType setDate setType
- 升级macOS系统版本后pod命令无效的问题
- 2 unit 8
- 浅析HTTPS中间人攻击与证书校验
- PHP内置函数array_reverse、array_reduce、call_user_func和call_user_func_array
- ajax数据回显同步
- 第十四周项目二 二叉树排序树中查找的路径
- 5个常用的MySQL数据库管理工具
- hadoop常用命令
- KMP next nextval
- mediastreamer使用教程
- 环境变量
- 程序打包成ipa后安装,点击App有一定概率会闪退