Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Saturnlai's avatar

How to use sum with groupBy multiple columns

I have an array with multiple levels and I want sum total_price on the keys Accessory, Apparel. Please help me if you have any ideas. Thanks My array photo-2022-11-26-11-23-25.jpg photo-2022-11-26-11-25-37.jpg

0 likes
6 replies
Tray2's avatar

Where do you get the data from?

Saturnlai's avatar

@Tray2 That's data after group by from columns (ref_venue_id, order_date, label, class) Here's the real data before groupped.

photo-2022-11-26-13-13-00.jpg

Here's the data i need to filled on the excel file photo-2022-11-26-13-16-22.jpg

If you have an idea. Please help me. Thanks so much!

Tray2's avatar

@Saturnlai I asked where does it come from,.

  • Does it come from the database?
  • What does the data look like in the source?
  • What is the desired result?
Saturnlai's avatar

@Tray2

  1. Yes, it's come from the database.

  2. The data after a query is an array.

Input

[
  1 => array:14 [
    "order_id" => 2524
    "uuid" => "PO-20220807-000014-001"
    "unit_price_without_vat" => 179000.0
    "total_without_vat" => 179000.0
    "units_total" => 1
    "venue_id" => 15
    "event_category" => "virtual_catalog"
    "ref_venue_id" => "58814"
    "event_name" => "Kansai"
    "label_division" => array:2 [ …2]
    "class" => "Apparel"
    "label" => "Collection"
    "source" => "online"
    "order_date" => "2022-08-07"
  ]
  2 => array:14 [
    "order_id" => 2524
    "uuid" => "PO-20220807-000014-002"
    "unit_price_without_vat" => 179000.0
    "total_without_vat" => 179000.0
    "units_total" => 1
    "venue_id" => 15
    "event_category" => "virtual_catalog"
    "ref_venue_id" => "58814"
    "event_name" => "Kansai"
    "label_division" => array:2 [ …2]
    "class" => "Apparel"
    "label" => "Collection"
    "source" => "online"
    "order_date" => "2022-08-07"
  ]
  3 => array:14 [
    "order_id" => 2524
    "uuid" => "PO-20220807-000014-003"
    "unit_price_without_vat" => 51000.0
    "total_without_vat" => 51000.0
    "units_total" => 1
    "venue_id" => 15
    "event_category" => "virtual_catalog"
    "ref_venue_id" => "58814"
    "event_name" => "Kansai"
    "label_division" => array:2 [ …2]
    "class" => "Apparel"
    "label" => "Collection"
    "source" => "online"
    "order_date" => "2022-08-07"
  ]
  4 => array:14 [
    "order_id" => 2528
    "uuid" => "PO-20220807-000018-001"
    "unit_price_without_vat" => 291000.0
    "total_without_vat" => 291000.0
    "units_total" => 1
    "venue_id" => 15
    "event_category" => "virtual_catalog"
    "ref_venue_id" => "58814"
    "event_name" => "Kansai"
    "label_division" => array:2 [ …2]
    "class" => "Accessory"
    "label" => "Collection"
    "source" => "offline"
    "order_date" => "2022-08-07"
  ]
  6 => array:14 [
    "order_id" => 2531
    "uuid" => "PO-20220807-000021-001"
    "unit_price_without_vat" => 145000.0
    "total_without_vat" => 145000.0
    "units_total" => 1
    "venue_id" => 15
    "event_category" => "virtual_catalog"
    "ref_venue_id" => "58814"
    "event_name" => "Kansai"
    "label_division" => array:2 [ …2]
    "class" => "Apparel"
    "label" => "Collection"
    "source" => "offline"
    "order_date" => "2022-08-07"
  ]
]
  1. The results I just want Output
[
  "58814"=>[
    "2022-09-16"=>[
      "Collection"=>[
        "Apparel"=>[
          "total_without_vat": 145000
        ],
        "Accessory"=>[
          "total_without_vat": 245000
        ]
      ],
      "RRL"=>[
        "Apparel"=>[
          "total_without_vat": 5000
        ],
        "Accessory"=>[
          "total_without_vat": 15000
        ]
      ]
    ],
    "2022-09-17"=>[
      "Collection"=>[
        "Apparel"=>[
          "total_without_vat": 125000
        ],
        "Accessory"=>[
          "total_without_vat": 3335000
        ]
      ],
      "RRL"=>[
        "Apparel"=>[
          "total_without_vat": 34000
        ],
        "Accessory"=>[
          "total_without_vat": 4000
        ]
      ]
    ]
  ]
]
Tray2's avatar

@Saturnlai You need to show the table structure, and examples of the data in those tables.

The array doesn't tell me anything, since you need a query that does that for you, and not process the data once again in php after you fetched it from the database.

Saturnlai's avatar

@tray2 In such a case I just have only an array with the data shared. Is that the process?

Please or to participate in this conversation.