我有一个多维数组的项目添加到cart在codeigniter。让我们假设我订购的食物为我和几个朋友(具体的ID存储在下一级数组中)。现在,如果有人有更多的项目,我需要得到每个朋友的总和,并保存为我拥有的钱。如何循环所有项目,以获得相同ID的每个朋友的总和(我不能将朋友ID移动到父数组)。我将它们存储到数据库中,存储方式如下表所示,不重复。我需要一个新数组来获得这样的结果(存储/更新它们)。
| 好友标识|拥有金额|
| - -|- -|
| 五十二|三十五|
| 二十八人|五个|
| friend_id 0是我...我们跳过我!= 0||
Array
(
[array] => Array
(
[carthashid1] => Array
(
[0] => Array
(
[foodid] => 322
[price] => 5
[name] => Chicken Burger
[options] => Array
(
[friend_id] => 52
[special_instructions] =>
)
[rowid] => ceec8698316fe95ec9d7dccf961f32c1
[num_added] => 5
[sum_price] => 25
)
[1] => Array
(
[foodid] => 323
[price] => 5
[name] => Beef Burger
[options] => Array
(
[friend_id] => 52
[special_instructions] =>
)
[rowid] => c2d1c15d159123d1cbdce967785ef06e
[num_added] => 2
[sum_price] => 10
)
[2] => Array
(
[foodid] => 322
[price] => 5
[name] => Chicken Burger
[options] => Array
(
[friend_id] => 28
[special_instructions] =>
)
[rowid] => 3daa7b14b23a5c0afa9b196ea6e35227
[num_added] => 1
[sum_price] => 5
)
[3] => Array
(
[foodid] => 323
[price] => 5
[name] => Beef Burger
[options] => Array
(
[friend_id] => 0
[special_instructions] =>
)
[rowid] => 734c9cc82cf35e2dcc42f28d96a8ebde
[num_added] => 1
[sum_price] => 5
)
)
)
[finalSum] => 45
[finalItemsCount] => 9
)
2条答案
按热度按时间xn1cxnb41#
如果我不需要对数组进行手工编码的话,我检查答案所花费的时间会更少,但无论如何,它还是出现了
结果
b5lpy0ml2#
理论上,您可以在单个DB查询中完成整个任务,也可以通过修改DB查询将friend_id移动到父数组。
保持
friend_id, amount_owned
结构的方法略有不同:结果: