I
I
Islam Ibakaev2021-10-30 16:35:02
Laravel
Islam Ibakaev, 2021-10-30 16:35:02

What manipulations to carry out over a collection to receive result of desirable structure?

Hello.
I have a collection of this kind (simplified for simplicity)

{
  "1": [
    {
      "id": "1",
      "qty": "4",
      "subtotal": "2400.00"
    },
    {
      "id": "2",
      "qty": "2",
      "subtotal": "4000.00"
    }
  ],
  "2": [
    {
      "id": "3",
      "qty": "2",
      "subtotal": "3000.00"
    }
  ]
}

Please tell me how to get the following result
{
  "1": {
    "qty": "6",
    "subtotal": "6400.00"
  },
  "2": {
    "qty": "2",
    "subtotal": "3000.00"
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-10-30
@devellopah

You need to use exactly two methods - mapWithKeys and sum (having previously wrapped the internal arrays in a collection).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question