PHP merge two arrays on the same key AND value
<p>I have two arrays. And I want to merge them on the same key AND value. If they have the same <code>ur_user_id</code> then they are merged. <code>array2</code> only provides some additional data for <code>array1</code>, so the <code>new_array.length</code> = <code>array1.length</code>. <code>array1</code> just gets the additional data from <code>array2</code>.</p>

<pre><code>$array1 =    
    array(
        array('ur_user_id'=&gt; 1,'ur_fname'=&gt;'PerA','ur_lname'=&gt;'SonA'),
        array('ur_user_id'=&gt; 2,'ur_fname'=&gt;'PerB','ur_lname'=&gt;'SonB'),
        array('ur_user_id'=&gt; 3,'ur_fname'=&gt;'PerC','ur_lname'=&gt;'SonC'),
    );
$array2 = 
    array(
        array('ur_user_id' =&gt; 5,'ur_code' =&gt; 'EE','ur_user_role' =&gt; 'testE'),
        array('ur_user_id' =&gt; 4,'ur_code' =&gt; 'DD','ur_user_role' =&gt; 'testD'),
        array('ur_user_id' =&gt; 6,'ur_code' =&gt; 'FF','ur_user_role' =&gt; 'testF'),
        array('ur_user_id' =&gt; 3,'ur_code' =&gt; 'CC','ur_user_role' =&gt; 'testC'),
        array('ur_user_id' =&gt; 1,'ur_code' =&gt; 'AA','ur_user_role' =&gt; 'testA'),
        array('ur_user_id' =&gt; 2,'ur_code' =&gt; 'BB','ur_user_role' =&gt; 'testB'),
    );    
</code></pre>

<p>Then the new array must look like this. It will have both the values from the <code>array1</code> and <code>array2</code>.</p>

<pre><code>$new_array =    
    array(
        array('ur_user_id'=&gt; 1,'ur_fname'=&gt;'PerA','ur_lname'=&gt;'SonA','ur_code' =&gt; 'AA','ur_user_role' =&gt; 'testA'),
        array('ur_user_id'=&gt; 2,'ur_fname'=&gt;'PerB','ur_lname'=&gt;'SonB','ur_code' =&gt; 'BB','ur_user_role' =&gt; 'testB'),
        array('ur_user_id'=&gt; 3,'ur_fname'=&gt;'PerC','ur_lname'=&gt;'SonC','ur_code' =&gt; 'CC','ur_user_role' =&gt; 'testC'),
    );
</code></pre>

<p>The <code>array1.length</code> is always less than or equal to <code>array2.length</code> never greater. And the order of both arrays will not be always ordered. I've tried the function below which I got somewhere here but it doesn't work for me and I'm not really good with loops.</p>

<pre><code>function merge_common_keys(){
    $arr = func_get_args();
    $num = func_num_args();

    $keys = array();
    $i = 0;
    for ($i=0; $i&lt;$num; ++$i){
        $keys = array_merge($keys, array_keys($arr[$i]));
    }
    $keys = array_unique($keys);

    $merged = array();

    foreach ($keys as $key){
        $merged[$key] = array();
        for($i=0; $i&lt;$num; ++$i){
            $merged[$key][] = isset($arr[$i][$key]) ? $arr[$i][$key] : null;
        }
    }
    return $merged;
}
</code></pre>

<p>Based on the given arrays the result is like this. It only merges on the same key.</p>

<pre><code>Array
(
    [0] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [ur_user_id] =&gt; 1
                    [ur_fname] =&gt; PerA
                    [ur_lname] =&gt; SonA
                )

            [1] =&gt; Array
                (
                    [ur_user_id] =&gt; 5
                    [ur_code] =&gt; AA-BB-CC
                    [ur_user_role] =&gt; testE
                )

        )
</code></pre>