php二维数组根据price值相加,和大于20的拆分成新的数组

吴祥
  • 2
新手上路,请多包涵

问题描述

php二维数组根据price值相加,和大于20的拆分成新的数组

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
Array
(

[0] => Array
    (
        [inventory_type] => 1
        [order_goods] => Array
            (
                [0] => Array
                    (
                        [id] => 162646434
                        [price] => 7.65
                        [goods_id] => 274774
                    )

                [1] => Array
                    (
                        [id] => 162646435
                        [price] => 12.46
                        [goods_id] => 445018
                    )

                [2] => Array
                    (
                        [id] => 162646436
                        [price] => 17.00
                        [goods_id] => 461913
                    )

                [3] => Array
                    (
                        [id] => 162646437
                        [price] => 10.68
                        [goods_id] => 408752
                    )

            )

    )

)

你期待的结果是什么?实际看到的错误信息又是什么?

Array
(

[0] => Array
    (
        [inventory_type] => 1
        [order_goods] => Array
            (
                [0] => Array
                    (
                        [id] => 162646434
                        [price] => 7.65
                        [goods_id] => 274774
                    )

                [1] => Array
                    (
                        [id] => 162646435
                        [price] => 12.46
                        [goods_id] => 445018
                    )
            )

    )
[1] => Array
    (
        [inventory_type] => 1
        [order_goods] => Array
            (
             
                [0] => Array
                    (
                        [id] => 162646436
                        [price] => 17.00
                        [goods_id] => 461913
                    )

            )

    )
[2] => Array
    (
        [inventory_type] => 1
        [order_goods] => Array
            (
             
                [0] => Array
                    (
                        [id] => 162646437
                        [price] => 10.68
                        [goods_id] => 408752
                    )

            )

    )    

)

回复
阅读 1k
1 个回答
吴祥
  • 2
新手上路,请多包涵
✓ 已被采纳

自己已经写出来了!谢谢大家支持!

public function getData($list,$inventory_type){

    $slice = array();
    $newList = array();
    while (!empty($list)) {
        $first = array_shift($list);
        array_push($slice, $first);
        if (array_sum(array_column($slice,'avg_price')) > 650) {
            $last = array_pop($slice);
            array_unshift($list, $last);
            $newList['inventory_type'] = $inventory_type;
            $newList['order_goods'][] = $slice;
            $slice = array();
        }
        if (empty($list) && !empty($slice)) {
            $newList['inventory_type'] = $inventory_type;
            $newList['order_goods'][] = $slice;
            $slice = array();
        }
    }
    return $newList;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏