Home > other >  Trying to get the sum of php array object in a nested loop
Trying to get the sum of php array object in a nested loop

Time:02-14

I know this is quite basic.

I'm trying to get the sum of qty for each of the keys from the below object but it doesn't return what I expect.

{
        "3": [
            {
                "id": 1,
                "qty": 4,
                "customer_id": 3,
                "cargo_id": 3,
                "customer": {
                    "id": 3,
                    "company_name": "Mertz and Sons",
                    "phone": "(586) 898-5987"
                },
                
            }
        ],
        "6": [
            {
                "id": 3,
                "qty": 9,
                "customer_id": 6,
                "cargo_id": 3,
                "customer": {
                    "id": 6,
                    "company_name": "Turcotte PLC",
                    "phone": " 13422302445"
                },
            },
            {
                "id": 4,
                "qty": 3,
                "customer_id": 6,
                "customer": {
                    "id": 6,
                    "company_name": "Turcotte PLC",
                    "phone": " 13422302445"
                },
                
            }
        ]
    }

Here is what I'm currently doing

$attrs = [];
foreach($record as $item=> $value){

            foreach($value as $k){
                $attrs['customer'] = $k['customer']['company_name'];
                $sum_ordered = 0;
                $sum_ordered  = $k['qty']; 
                $attrs['ordered_sum'] = $sum_ordered;
            }
            $output[] = $attrs;
}
echo json_encode($output);

and this is the result I get, I can't figure out what is not right. I'm expecting 4 and 12 respectively as ordered_sum

[
   {"customer":"Mertz and Sons","ordered_sum":4},
   {"customer":"Turcotte PLC","ordered_sum":3}
]

CodePudding user response:

Try this

<?php

$record = '{
    "3": [
        {
            "id": 1,
            "qty": 4,
            "customer_id": 3,
            "customer": {
                "id": 3,
                "company_name": "Mertz and Sons",
                "phone": "(586) 898-5987"
            }
        }
    ],
        "5": {
            "1": {
                "id": 2,
                "qty": 8,
                "customer_id": 5,
                "customer": {
                    "id": 5,
                    "company_name": "Reichert, Witting and Durgan",
                    "phone": "961.352.6817 x0265"
                }
            }
        },
        "6": {
            "2": {
                "id": 3,
                "qty": 9,
                "customer_id": 6,
                "customer": {
                    "id": 6,
                    "company_name": "Turcotte PLC",
                    "phone": " 13422302445"
                }
            },
            "3": {
                "id": 4,
                "qty": 3,
                "customer_id": 6,
                "customer": {
                    "id": 6,
                    "company_name": "Turcotte PLC",
                    "phone": " 13422302445"
                }
            }
        }
}';


$record = json_decode($record, true);

$output = [];
foreach($record as $item=> $value){
    $attrs = [
        'customer'    => '',
        'ordered_sum' => 0
    ];

    foreach($value as $k){
        $attrs['customer']     = $k['customer']['company_name'];
        $attrs['ordered_sum']  = $k['qty'];
    }

    $output[] = $attrs;
}

echo json_encode($output);

CodePudding user response:

I assume that you are using Laravel

so you can utilize the collection function.

collect($array)
  ->flatten(1) // flatten the unnecessary array 
  ->groupBy(function($item) { // group by company name
       return $item['customer']['company_name'];
  })->map(function($items, $key) { // remap according to your needs
       return [
           'customer' => $key,
           'ordered_sum' => collect($items)->sum('qty'), // sum the qty
       ];
   })
   ->values() // remove keys
   ->toArray();

Output

=> [
 [
   "customer" => "Mertz and Sons",
   "ordered_sum" => 4,
 ],
 [
   "customer" => "Reichert, Witting and Durgan",
   "ordered_sum" => 8,
 ],
 [
   "customer" => "Turcotte PLC",
   "ordered_sum" => 12,
 ],
]
  • Related