Search code examples
phparraysmagento

Merge 2 Multidimensional Arrays


I am working on formatting an array for a Magento soap call. I have two mysql queries and ARRAY1 and ARRAY2 are the results. How would I merge/combine ARRAY1 and ARRAY2 so that the results are like ARRAY3?

I appreciate any help.

ARRAY1

<pre>
array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "store1"
    [1]=>
    string(8) "product1"
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "store1"
    [1]=>
    string(8) "product2"
  }
  [2]=>
  array(2) {
    [0]=>
    string(6) "store2"
    [1]=>
    string(8) "product2"
  }
}
</pre>

ARRAY2

<pre>
array(3) {
  [0]=>
  array(1) {
    ["status"]=>
    string(3) "Yes"
  }
  [1]=>
  array(1) {
    ["status"]=>
    string(2) "No"
  }
  [2]=>
  array(1) {
    ["status"]=>
    string(3) "Yes"
  }
}
</pre>

ARRAY3

<pre>
array (size=3)
  0 => 
    array (size=3)
      0 => string 'store1' (length=6)
      1 => string 'product1' (length=8)
      2 => 
          array (size=1)
           'status' => string 'Yes' (length=3)
  1 => 
    array (size=3)
      0 => string 'store1' (length=6)
      1 => string 'product2' (length=8)
      2 => 
          array (size=1)
           'status' => string 'No' (length=2)
  2 => 
    array (size=3)
      0 => string 'store2' (length=6)
      1 => string 'product2' (length=8)
      2 => 
          array (size=1)
           'status' => string 'No' (length=2)
</pre>

Solution

  • Assuming the two arrays are lined up perfectly, this may help:

    <?php
    $i = 0; 
    foreach ($array1 as $item) {
    array_push($item, $array2[$i]);
    $i++;
    } 
    ?>