collection中sum求和

计算下列数组中order_products下数组元素price的和

开始前,使用 composer require illuminate/support,获得collect的相关支持。

<?php
// 引入package
require __DIR__ . '/vendor/autoload.php';

$orders = [[
    'id'            =>      1,
    'user_id'       =>      1,
    'number'        =>      '13908080808',
    'status'        =>      0,
    'fee'           =>      10,
    'discount'      =>      44,
    'order_products'=> [
        ['order_id'=>1,'product_id'=>1,'param'=>'6寸','price'=>555.00,'product'=>['id'=>1,'name'=>'蛋糕名称','images'=>[]]],
        ['order_id'=>1,'product_id'=>1,'param'=>'7寸','price'=>333.00,'product'=>['id'=>1,'name'=>'蛋糕名称','images'=>[]]],
    ],
]];

使用foreach循环数据

$price = 0;
foreach ($orders as $order) {
    foreach($order['order_products'] as $ordert_product){
        $price += $ordert_product['price'];
    }
}
dd($price);

声明临时变量记录总价格,配合两次循环获得总价并累计。

将上面的方式改成collection的map、flatten和sum方式

collect($orders)->map(function($order){
    return $order['order_products'];
})->flatten(1)->map(function($order){
    return $order['price'];
})->sum();

将上面的求解方式改成collection的flatMap、pluck和sum方式

collect($orders)->flatMap(function($order){
    return $order['order_products'];
})->pluck('price')->sum();

将上面的求解方式改成collection的flatMap和sum方式

collect($orders)->flatMap(function($order){
    return $order['order_products'];
})->sum('price');
Copyright © http://blog.webfsd.com 2018 all right reserved,powered by Gitbook该文件修订时间: 2019-05-21 04:55:26

results matching ""

    No results matching ""