1

I have the following two arrays of objects:

First Array: $array1

Array
(
    [0] => stdClass Object
        (
            [pid] => 1
            [pname] => iphone-6s
            [product_category] => 1
            [product_price] => 250
            [product_description] => Its iphone-6s, Apple Brand Products
            [product_image] => iPhone6s.png
            [uid] => 1
            [product_status] => Active
        )

    [1] => stdClass Object
        (
            [pid] => 2
            [pname] => Beauty Care
            [product_category] => 1
            [product_price] => 200
            [product_description] => Beauty Care's Products
            [product_image] => 2ca65f58e35d9ad45bf7f3ae5cfd08f1_beauty-care.png
            [uid] => 1
            [product_status] => Active
        )

    [2] => stdClass Object
        (
            [pid] => 3
            [pname] => iphone-4
            [product_category] => 1
            [product_price] => 230
            [product_description] => its iphone-4, Apple Brand Products
            [product_image] => 5878a7ab84fb43402106c575658472fa_iphone_4.png
            [uid] => 1
            [product_status] => Active
        )

    [3] => stdClass Object
        (
            [pid] => 4
            [pname] => Android tv
            [product_category] => 1,2
            [product_price] => 180
            [product_description] => Smart TV
            [product_image] => Android-TV.png
            [uid] => 2
            [product_status] => Active
        )

    [4] => stdClass Object
        (
            [pid] => 5
            [pname] => AC
            [product_category] => 1
            [product_price] => 185
            [product_description] => AC
            [product_image] => air_conditioner.png
            [uid] => 2
            [product_status] => Active
        )

    [5] => stdClass Object
        (
            [pid] => 6
            [pname] => Harry Potter
            [product_category] => 4
            [product_price] => 25
            [product_description] => Harry Potter and the Cursed Child
            [product_image] => harry_potter.jpg
            [uid] => 1
            [product_status] => Active
        )

    [6] => stdClass Object
        (
            [pid] => 7
            [pname] => Football
            [product_category] => 4
            [product_price] => 25
            [product_description] => Nivia Football
            [product_image] => football.jpeg
            [uid] => 1
            [product_status] => Active
        )

    [7] => stdClass Object
        (
            [pid] => 8
            [pname] => Ps4
            [product_category] => 1,3,4,5
            [product_price] => 1600
            [product_description] => Ps4
            [product_image] => Ps-4.png
            [uid] => 2
            [product_status] => Active
        )

)

Second Array: $array2

Array
(
    [0] => stdClass Object
        (
            [mysummary] => Electronics
        )

    [1] => stdClass Object
        (
            [mysummary] => Electronics,Fashion
        )

    [2] => stdClass Object
        (
            [mysummary] => Electronics
        )

    [3] => stdClass Object
        (
            [mysummary] => Sports, Books and Gaming etc.
        )

    [4] => stdClass Object
        (
            [mysummary] => Sports, Books and Gaming etc.
        )

    [5] => stdClass Object
        (
            [mysummary] => Electronics
        )

    [6] => stdClass Object
        (
            [mysummary] => Electronics
        )

    [7] => stdClass Object
        (
            [mysummary] => Electronics,Home & Furniture,Sports, Books and Gaming etc.,Beauty & Personal Care
        )

)

I want to merge these two object arrays

Desired output:

Array
(
    [0] => stdClass Object
        (
            [pid] => 1
            [pname] => iphone-6s
            [product_category] => 1
            [product_price] => 250
            [product_description] => Its iphone-6s, Apple Brand Products
            [product_image] => iPhone6s.png
            [uid] => 1
            [product_status] => Active
            [mysummary] => Electronics
        )

    [1] => stdClass Object
        (
            [pid] => 2
            [pname] => Beauty Care
            [product_category] => 1,2
            [product_price] => 200
            [product_description] => Beauty Care's Products
            [product_image] => 2ca65f58e35d9ad45bf7f3ae5cfd08f1_beauty-care.png
            [uid] => 1
            [product_status] => Active
            [mysummary] => Electronics,Fashion
        )

    [2] => stdClass Object
        (
            [pid] => 3
            [pname] => iphone-4
            [product_category] => 1
            [product_price] => 230
            [product_description] => its iphone-4, Apple Brand Products
            [product_image] => 5878a7ab84fb43402106c575658472fa_iphone_4.png
            [uid] => 1
            [product_status] => Active
            [mysummary] => Electronics
        )

    [3] => stdClass Object
        (
            [pid] => 4
            [pname] => Android tv
            [product_category] => 3,4
            [product_price] => 180
            [product_description] => Smart TV
            [product_image] => Android-TV.png
            [uid] => 2
            [product_status] => Active
             [mysummary] => Sports, Books and Gaming etc.
        )

    [4] => stdClass Object
        (
            [pid] => 5
            [pname] => AC
            [product_category] => 3,4
            [product_price] => 185
            [product_description] => AC
            [product_image] => air_conditioner.png
            [uid] => 2
            [product_status] => Active
            [mysummary] => Sports, Books and Gaming etc.
        )

    [5] => stdClass Object
        (
            [pid] => 6
            [pname] => Harry Potter
            [product_category] => 1
            [product_price] => 25
            [product_description] => Harry Potter and the Cursed Child
            [product_image] => harry_potter.jpg
            [uid] => 1
            [product_status] => Active
            [mysummary] => Electronics
        )

    [6] => stdClass Object
        (
            [pid] => 7
            [pname] => Football
            [product_category] => 1
            [product_price] => 25
            [product_description] => Nivia Football
            [product_image] => football.jpeg
            [uid] => 1
            [product_status] => Active
            [mysummary] => Electronics
        )

    [7] => stdClass Object
        (
            [pid] => 8
            [pname] => Ps4
            [product_category] => 1,3,4,5
            [product_price] => 1600
            [product_description] => Ps4
            [product_image] => Ps-4.png
            [uid] => 2
            [product_status] => Active
            [mysummary] => Electronics,Home & Furniture,Sports, Books and Gaming etc.,Beauty & Personal Care
        )

)
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36
  • @RahulMeshram its merge the array like this : https://prnt.sc/mq8e36 – RïshïKêsh Kümar Feb 26 '19 at 08:52
  • Related: [Two arrays in foreach loop](https://stackoverflow.com/q/4480803/2943403) Simply calling `array_merge()` or `array_merge_recursive()` will not work -- these techniques do not work with objects. – mickmackusa Sep 26 '22 at 21:49

3 Answers3

2

You can cycle using an index and add the attribute of the second array to the first:

for ($index = 0; $index < count($array1); $index++) {
    $array1[$index]->mysummary = $array2[$index]->mysummary;
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
2
foreach ($array1 as $key => $value) {
    $array1[$key]->mysummary = $array2[$key]->mysummary;
}
echo '<pre>';print_r($array1);
mujuonly
  • 11,370
  • 5
  • 45
  • 75
1

You can use a combination of array_map and array_merge to achieve the same without any dependency of a static key of arrays.

$result = array_map(function($a,$b){  
    return array_merge(isset($a) ? $a : array(), isset($b) ? $b : array());
},$a1,$a2);  

Demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60