I
I
Islam Ibakaev2022-03-03 10:26:20
Laravel
Islam Ibakaev, 2022-03-03 10:26:20

How to merge the contents of two collections?

There are two collections( $itemsByMart , $customProductsByMart ). Below is the screenshot of the code

return [
            '$itemsByMart' => $itemsByMart,
            '$customProductsByMart' => $customProductsByMart
        ];

62206be5d2b05143004571.png

I want to combine all the contents of two collections into one, but merge is not suitable, because I need the following output
{
  "1": [
     {...},
     {...},
     {...}
  ],
  "2": [
     {...}
  ],
  "3": [
     {...}
  ]
}

Please tell me how to get this result.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2022-03-03
@devellopah

One new empty collection and two foreach - one for each existing one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question