codeigniter 获取存储在多维数组中的重复ID的总和

sqserrrh  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(120)

我有一个多维数组的项目添加到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
)
xn1cxnb4

xn1cxnb41#

如果我不需要对数组进行手工编码的话,我检查答案所花费的时间会更少,但无论如何,它还是出现了

$input = [
    'array' => [
        'carthashid1' => [
            [
                'foodid' => 322, 'price' => 5,
                'name' => 'Chicken Burger', 
                'options' => ['friend_id' => 52, 'special_instructions' => ''],
                'rowid' => 'ceec8698316fe95ec9d7dccf961f32c1', 'num_added' => 5,'sum_price' => 25
            ],
            [
                'foodid' => 322, 'price' => 5,
                'name' => 'Beef Burger',
                'options' => ['friend_id' => 52,'special_instructions' => ''],
                'rowid' => 'ceec8698316fe95ec9d7dccf961f32c1', 'num_added' => 2,'sum_price' => 10
            ],
            [
                'foodid' => 322,'price' => 5,'name' => 'Chicken Burger',
                'options' => ['friend_id' => 28,'special_instructions' => ''],
                'rowid' => 'ceec8698316fe95ec9d7dccf961f32c1', 'num_added' => 1,'sum_price' => 5
            ],
            [
                'foodid' => 322, 'price' => 5, 'name' => 'Beef Burger',
                'options' => ['friend_id' => 0,'special_instructions' => ''],
                'rowid' => 'ceec8698316fe95ec9d7dccf961f32c1', 'num_added' => 1, 'sum_price' => 5
            ]
        ]
    ]
];

$friends = [];
foreach($input['array']['carthashid1']  as $ordered){
    if ($ordered['options']['friend_id'] == 0) {
        // its me, ignore me
        continue;
    }
    if ( ! isset($friends[$ordered['options']['friend_id']]) ) {
        // initialise the accumulator for this friend
        $friends[$ordered['options']['friend_id']] = 0; 
    } 
    $friends[$ordered['options']['friend_id']] += $ordered['sum_price'];    
}

print_r($friends);

结果

Array
(
    [52] => 35
    [28] => 5
)
b5lpy0ml

b5lpy0ml2#

理论上,您可以在单个DB查询中完成整个任务,也可以通过修改DB查询将friend_id移动到父数组。
保持friend_id, amount_owned结构的方法略有不同:

$owned = [];
foreach($arr['array']['carthashid1'] as $item){
    if(isset($item['options']['friend_id']) && $item['options']['friend_id'] != 0)
    {
        if(count($owned) && ($key = array_search($item['options']['friend_id'], array_column($owned, 'friend_id'))) !== false){
            // we found friend id in array lets add the price:
            $owned[$key]['amount_owned'] += $item['sum_price'];
            continue;
        }
        // when we dont find the friend id in array create that item here:
        $owned[] = ['friend_id' => $item['options']['friend_id'], 'amount_owned' => $item['sum_price']];
    }
}

print_r($owned);

结果:

Array
(
    [0] => Array
        (
            [friend_id] => 52
            [amount_owned] => 35
        )

    [1] => Array
        (
            [friend_id] => 28
            [amount_owned] => 5
        )
)

相关问题