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

Get total sum of repeating IDs stored in multidimensional array

我在 codeigniter 的购物车中添加了一个多维项目数组。假设我为我和几个朋友点了食物(特定的 ID 存储在下一级数组中)。现在,如果有人有更多物品,我需要获得每个朋友的总和并将其保存为我拥有的钱。如何循环所有项目以获得具有相同 ID 的每个朋友的总和(我无法将朋友 ID 移动到父数组)。我以我们可以在下面的 table 中看到的非重复方式将它们存储到数据库中。我需要一个新数组来获得这样的结果(store/update)。

friend_id amount_owned
52 35
28 5
friend_id 0 is me...we skip me != 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
)

如果我不必手动对数组进行编码,检查我的答案会花费更少的时间,但无论如何它就在这里

$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
)

理论上您可以在单个数据库查询中完成整个任务,也可以通过修改数据库查询将 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
        )
)