3

i am new in laravel. i want to merge 2 multidimansional array in one array. i am using array_merge function but isn't work. i want merge array[0] position values and array[1] position values.

here is my current array look like this

Array
(
  [0] => Illuminate\Support\Collection Object
      (
          [items:protected] => Array
              (
                  [0] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-15 16:33:59
                          [name] => Tea
                          [totalqty] => 3
                      )
                  [1] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-16 10:44:14
                          [name] => Tea
                          [totalqty] => 5
                      )
                 )
      )
  [1] => Illuminate\Support\Collection Object
      (
          [items:protected] => Array
              (
                  [0] => stdClass Object
                      (
                          [product_id] => 3
                          [sale_datetime] => 2018-11-15 18:04:36
                          [name] => Coffee
                          [totalqty] => 20
                      )
              )
      )
)

and i want to make array like below array

Array
(
  [0] => Illuminate\Support\Collection Object
      (
          [items:protected] => Array
              (
                  [0] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-15 16:33:59
                          [name] => Tea
                          [totalqty] => 3
                      )

                  [1] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-16 10:44:14
                          [name] => Tea
                          [totalqty] => 5
                      )
                  [2] => stdClass Object
                      (
                          [product_id] => 3
                          [sale_datetime] => 2018-11-15 18:04:36
                          [name] => Coffee
                          [totalqty] => 20
                      )
                 )
      )
)
Bhoomi Patel
  • 777
  • 10
  • 32

3 Answers3

0

The output of your code is an object not a direct array, use foreach loop and then use array_merge.

Eg. Suppose your output is stored in variable $array

foreach($array as $arrays){
  array_merge('$arrays', $array_you_want_to_merge);
}

Note:- Your code structure is not clear so might be it will not work, but it will will give you an idea.

Ajit Maurya
  • 56
  • 1
  • 9
0

try this code

$a = $arr[0];
$b = $arr[1];
$newArr= array();
$newArr["items:protected"] = array_merge($a["items:protected"],$b["items:protected"]);
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
0

You're using Collections from laravel. While you can use the default PHP functions like array_merge, why not use the laravel way, by using merge.

Copy + pasted from the documentation:

$collection = collect(['product_id' => 1, 'price' => 100]);

$merged = $collection->merge(['price' => 200, 'discount' => false]);

$merged->all();

// ['product_id' => 1, 'price' => 200, 'discount' => false]

OR if you wish to merge 2 collections:

$original = new Collection(['foo']);

$latest = new Collection(['bar']);

$merged = $original->merge($latest); // Contains foo and bar.

Taken from here

Andrei
  • 3,434
  • 5
  • 21
  • 44