PHP merging two arrays, improving the code
<p>I have the following tested code that after a couple of tries does the job to filter unique results out of two merged arrays. I previously tried using array_diff, array_diff_key, array_diff_assoc to no avail until I settled with the following.</p>

<ul>
<li>The second needs to first exclude empty values (like 0) using array_filter, otherwise empty values ("red" => "0") are also printed in the merged result which is not expected. </li>
<li>array_unique removes duplicates from the merge.</li>
</ul>

<p>So the following is working as expected, but I wonder if there is any betterment to the code which looks a bit like winding around. I meant I am not confident if the code is properly done:</p>

<pre><code>$array1 = array("a" =&gt; "green", "b" =&gt; "purple", "blue" =&gt; "0");
$array2 = array("a" =&gt; "0", "b" =&gt; "purple", "blue" =&gt; "blue", "red" =&gt; "0");

$merge  = array_unique(array_merge($array1, array_values(array_filter($array2))));
</code></pre>

<p>Any betterment is much appreciated. Thanks</p>

<p>UPDATE:
Sorry, I was evolving late in the requirement, that later I realized I should also consider replacing the first array1 with array2 even when NULL return when pairs exists in both. I had better go back to my drawing room. Thanks</p>
