問題描述
php二維數(shù)組根據(jù)price值相加,和大于20的拆分成新的數(shù)組
相關(guān)代碼
// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)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
)
)
)
)
你期待的結(jié)果是什么?實際看到的錯誤信息又是什么?
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
)
)
)
)
1 回答

九州編程
TA貢獻(xiàn)1785條經(jīng)驗 獲得超4個贊
自己已經(jīng)寫出來了!謝謝大家支持!
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;
}
- 1 回答
- 0 關(guān)注
- 334 瀏覽
添加回答
舉報
0/150
提交
取消