How to merge two JSON arrays in PHP based on a key
<p>I have two arrays </p>

<pre><code>array1 = array(
  {'id' =&gt; '1', 'name' =&gt; 'A'},
  {'id' =&gt; '2', 'name' =&gt; 'B'},
  {'id' =&gt; '3', 'name' =&gt; 'C'},
  {'id' =&gt; '4', 'name' =&gt; 'D'}
);

array2 = array(
  {'id' =&gt; '1', 'flag' =&gt; '1'},
  {'id' =&gt; '3', 'flag' =&gt; '0'}
);
</code></pre>

<p>I need to merge them based on the 'id' key , so the result should be </p>

<pre><code>array1 =  array(
{'id' =&gt; '1', 'name' =&gt; 'A', 'flag' =&gt; '1'},
{'id' =&gt; '2', 'name' =&gt; 'B'},
{'id' =&gt; '3', 'name' =&gt; 'C', 'flag' =&gt; '0'},
{'id' =&gt; '4', 'name' =&gt; 'D'}
);
</code></pre>

<p>any help ?</p>