Home > Enterprise >  Get total sum of repeating IDs stored in multidimensional array
Get total sum of repeating IDs stored in multidimensional array

Time:04-14

I have a multidimensional array of items added to cart in codeigniter. Lets say I order food for me and several friends (specific IDs stored in next level array). Now in case someone has more items I need to get total sum of each friend and save it as money owned to me. How to loop all items to get total sum of each friend with same ID (I cannot move the friend ID to parent array). I store them to database in a way we can see in the table below in non-repeating way. I need a new array to get results like this(to store/update them).

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
)

CodePudding user response:

It would have taken me less time to check my answer if I didnt have to Hand Code the array, but here it is anyway

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

RESULT


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

CodePudding user response:

You could theoretically do entire task in single DB query, also the friend_id can be moved to parent array by modifying DB query.

Slightly different approach to keep friend_id, amount_owned structure:

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

Result:

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

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