title: Merge rows between two arrays of objects based on column value
body: <p>After I merged two arrays like this <code>array_merge($array1, $array2);</code>, it becomes like this:</p>
  <pre><code>array(10) {
    [0]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;1&quot;
      [&quot;text&quot;]=&gt;
      string(5) &quot;one&quot;
    }
    [1]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;2&quot;
      [&quot;text&quot;]=&gt;
      string(8) &quot;two&quot;
    }
    [2]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;3&quot;
      [&quot;text&quot;]=&gt;
      string(4) &quot;three&quot;
    }
    [3]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;4&quot;
      [&quot;text&quot;]=&gt;
      string(8) &quot;four&quot;
    }
    [4]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;5&quot;
      [&quot;text&quot;]=&gt;
      string(3) &quot;five&quot;
    }
    [5]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;1&quot;
      [&quot;unit&quot;]=&gt;
      string(1) &quot;0&quot;
    }
    [6]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;2&quot;
      [&quot;unit&quot;]=&gt;
      int(0)
    }
    [7]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;3&quot;
      [&quot;unit&quot;]=&gt;
      int(0)
    }
    [8]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;4&quot;
      [&quot;unit&quot;]=&gt;
      string(1) &quot;0&quot;
    }
    [9]=&gt;
    object(stdClass) (2) {
      [&quot;id&quot;]=&gt;
      string(1) &quot;5&quot;
      [&quot;unit&quot;]=&gt;
      int(1)
    }
  }
  </code></pre>
  <p>Which means both arrays are literally merged. But what I wanted is since both arrays has common property called <code>id</code> and same value for it, it should become like:</p>
  <pre><code>array(2) {
    [0]=&gt;
      object(stdClass) (2) {
        [&quot;id&quot;]=&gt;
        string(1) &quot;1&quot;
        [&quot;text&quot;]=&gt;
        string(5) &quot;one&quot;
        [&quot;unit&quot;]=&gt;
        int(0)
      }
      [1]=&gt;
      object(stdClass) (2) {
        [&quot;id&quot;]=&gt;
        string(1) &quot;2&quot;
        [&quot;text&quot;]=&gt;
        string(8) &quot;two&quot;
        [&quot;unit&quot;]=&gt;
        int(2)
      }
    }
  </code></pre>
  <p><strong>Note that array1 has <code>id</code>, text while array2 has <code>id</code> and <code>unit</code>.</strong></p>
  <p>I did refer here as tried the first answer which suggest to use <code>array_map()</code>, but for me I'm getting error saying <em>argument 1 is not an array</em>.</p>
  <p><a href="https://stackoverflow.com/questions/26777245/combine-two-array-in-to-single-array-with-common-value">Combine two arrays into a single array based on a common column value</a></p>
  <p>EDIT:</p>
  <p>tried this (doesn't work):</p>
  <pre><code>$array1 = array_walk($array1, function(&amp;$value) { $value = (array) $value; })
  $array2 = array_walk($array2, function(&amp;$value) { $value = (array) $value; })
  function modifyArray($a, $b)
  {
      if (!empty($a) &amp;&amp; !empty($b)) {
          return array_merge($a, $b);
      } else if (!empty($a) &amp;&amp; empty($b)) {
          return $a;
      }  else if (empty($a) &amp;&amp; !empty($b)) {
          return $b;
      }
  }
  
  $new = array_map(&quot;modifyArray&quot;, $array1, $array2);
  var_dump($new);
  </code></pre>