206 php array_reduce的使用

15 篇文章 0 订阅

mixed array_reduce ( array $input , callable $function [, mixed $initial = NULL ] ) 使用回调函数迭代地将数组简化为单一的值,而array_map()则返回一个长度和原数组相同的数组。

它接受三个参数,
- 第一个是待处理的数组 $input
- 第二个是迭代执行的函数 $function,它接受两个参数,第一个参数是上一次迭代执行的返回值,第二个参数是本次迭代对应的数组中的元素
- 如果指定了可选参数 $initial,该参数将被当成是数组中的第一个值来处理,或者如果数组为空的话就作为最终返回值。

一个简单的例子

对一个数组的每一项求和

$arr = [
    1,2,3,4,5,6,7,11
];
$sum = 0;

foreach ($arr as $value) {
    $sum+= $value;
}

array_reduce()改写

$sum = array_reduce($arr,function($rsult_of_last,$new_item_of_array){
    $result_of_this = $rsult_of_last+$new_item_of_array;
    return $result_of_this;
});
echo $sum;

复杂一点的例子

我们想依次执行一组类的特定方法

$arr = [
    [a::class,'handle'],
    [b::class,'handle'],
    [c::class,'handle']
];

$method = function($result_of_last,$item_of_array){
    $object = new $item_of_array[0];
    $object->{$item_of_array[1]}();

    $result_of_this = $result_of_last++;
    return $result_of_this;
};


class a {
    function handle(){
        echo "handle step a".'<br/>';
    }
}

class b {
    function handle(){
        echo "handle step b".'<br/>';
    }
}

class c{
    function handle(){
        echo "handle step c".'<br/>';
    }
}

$result = array_reduce($arr,$method);
echo $result;

会返回

handle step a
handle step b
handle step c

handle()加上参数

$arr = [
    [a::class,'handle'],
    [b::class,'handle'],
    [c::class,'handle']
];

$method = function($result_of_last,$item_of_array){
   return call_user_func($item_of_array,$result_of_last);
};


class a {
    function handle($request){
        $request++;
        echo "handle step a".'<br/>';
        return $request;
    }
}

class b {
    function handle($request){
        $request++;

        echo "handle step b".'<br/>';
        return $request;
    }
}

class c{
    function handle($request){
        $request++;

        echo "handle step c".'<br/>';
        return $request;
    }
}

$result = array_reduce($arr,$method);
echo $result;

运行结果

handle step a
handle step b
handle step c
3

使用闭包

$arr = [
    [a::class,'handle'],
    [b::class,'handle'],
    [c::class,'handle']
];

$method = function($result_of_last,$item_of_array){

    return function ($passable) use ($item_of_array,$result_of_last){
        return call_user_func($item_of_array,$result_of_last,$passable);
    };
};

class a {
    function handle(Closure $next,$request){
        $request++;
        echo "handle step a".'<br/>';
        return $next($request);
    }
}

class b {
    function handle(Closure $next,$request){
        $request++;

        echo "handle step b".'<br/>';
        return $next($request);
    }
}

class c{
    function handle(Closure $next,$request){
        $request++;

        echo "handle step c".'<br/>';
        return $next($request);
    }
}


$destination = function ($request) {
    echo "解析request".'<br/>';

    return ++$request;
};

$firstSlice = function ($passable) use ($destination) {
    return call_user_func($destination, $passable);
};

$request =  5;

$result = call_user_func(
    array_reduce(array_reverse($arr),$method,$firstSlice),$request
);
echo $result;

此处使用了闭包层层嵌套,每个handle()都会递归返回return $next($request);,构建了栈结构,根据栈后进先出的特性,array_reverse($arr)逆转数组顺序,因此运行之后得到结果

handle step a
handle step b
handle step c
解析request
9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值