dipsy6900
dipsy6900

Reputation: 13

Merging couple different arrays with same keys into one array

I have 2 different arrays with the same array keys but different values, trying to merge them into 1 but and the values to fall under correct keys, wasn't sure on how to achieve this. here is my PHP code I use for both arrays.

    foreach($apiS->sku as $skuname=>$skuvalue) {
      $skus1[$skuname] = array(
       'promotion_price' => $skuvalue->price->priceText,
       'quantity' => $skuvalue->quantity,
       );
    }
    foreach($apiD->sku as $skuname=>$skuvalue) {
      $skus2[$skuname] = array(
       'price' => $skuvalue->price->priceText,
       );
    }

and this follows the result of both arrays

[skus1] => stdClass Object
(
    [0] => stdClass Object
        (
            [promotion_price] => 69
            [quantity] => 176
        )

    [3927138782588] => stdClass Object
        (
            [promotion_price] => 69
            [quantity] => 176
        )
)
[skus2] => stdClass Object
(
    [0] => stdClass Object
        (
            [price] => 138.00
        )
    [3927138782588] => stdClass Object
        (
            [price] => 138.00
        )
)

And my expected result would be like this:

[skus] => stdClass Object
(
    [0] => stdClass Object
        (
            [promotion_price] => 69
            [quantity] => 176
            [price] => 138.00
        )

    [3927138782588] => stdClass Object
        (
            [promotion_price] => 69
            [quantity] => 176
            [price] => 138.00
        )
)

Any ideas how to get this would be highly appreciated. Thanks

Upvotes: 1

Views: 74

Answers (1)

Yasin Patel
Yasin Patel

Reputation: 5731

If you want merge two records in single array take one array variable and change your loop as below.

$skus = [];
foreach($apiS->sku as $skuname=>$skuvalue) {
  $skus[$skuname]['promotion_price'] =  $skuvalue->price->priceText;
  $skus[$skuname]['quantity'] =  $skuvalue->quantity;
}
foreach($apiD->sku as $skuname=>$skuvalue) {
  $skus[$skuname]['price'] = $skuvalue->price->priceText;
}

Upvotes: 2

Related Questions