Combining two unequal arrays using key mappings

I would like to combine the two arrays below into one. More specifically, I want to add the contents of the second array to the corresponding key in the first array. The keys in the final array must have the contents of the matching keys of both arrays.

Array ( 
[123456789_404045862944400] => 192 
[123456789_403274909688162] => 186 
[123456789_402735273075459] => 311 
[123456789_252948031457462] => 385 
[123456789_400606749954978] => 287 
[123456789_286755318061725] => 358 
[123456789_399687880046865] => 257 
[123456789_398332190182434] => 240 
[123456789_397768486905471] => 311 
[123456789_396907650324888] => 293 
[123456789_394850557197264] => 496 
[123456789_394121230603530] => 475 
[123456789_369757766367627] => 488 
[123456789_391602517522068] => 506 
[123456789_390848830930770] => 437 
[123456789_389975351018118] => 452 
[123456789_242486689170043] => 525 
[123456789_388151047867215] => 415 
[123456789_387476447934675] => 502 
[123456789_386620518020268] => 467 
[123456789_215937481836499] => 359 
)

Array (
[123456789_404045862944400] => 23:52 
[123456789_403274909688162] => 22:21 
[123456789_402735273075459] => 04:29 
[123456789_252948031457462] => 06:22 
[123456789_400606749954978] => 05:01 
[123456789_286755318061725] => 04:51 
[123456789_399687880046865] => 21:51 
[123456789_398395260176127] => 01:13 
[123456789_398332190182434] => 23:19 
[123456789_397768486905471] => 05:38 
[123456789_397509266931393] => 00:46 
[123456789_396907650324888] => 03:38 
[123456789_394850557197264] => 05:12 
[123456789_394121230603530] => 04:15 
[123456789_369757766367627] => 04:01 
[123456789_391602517522068] => 03:44 
[123456789_390848830930770] => 06:05 
[123456789_389975351018118] => 04:00 
[123456789_242486689170043] => 04:13 
[123456789_388151047867215] => 00:22 
[123456789_387544787927841] => 07:34 
[123456789_387476447934675] => 04:51 
[123456789_386620518020268] => 06:05 
[123456789_386504878031832] => 02:38 
[123456789_215937481836499] => 01:10 
) 

      

What I've tried so far:

$array1 = array_merge($array1, $array2);

      

Also tried something like:

foreach($arr2 as $k=>$v) {
    $a[$k] = $arr1[$k];
}

      

But it does not concatenate / concatenate arrays with correct key match.

I also tried it with array_combine

, but since it creates an array using one array for the keys and another for its values, I couldn't get this to work.

+3


source to share


1 answer


Ok so I have two data arrays and an empty one to store the new array

$array1 = array('key1'=>'value1');
$array2 = array('key2'=>'value2');
$array3 = array();

      

To combine them based on a key

foreach($array1 as $k=>$v)
{
    if(array_key_exists($k, $array2))
    {
        $array3[$k] = array($v, $array2[$k]);
    }
}

      

$ array3 contains a new array. If you want to overwrite the original array, just add



$array1 = $array3;

      

And if you want array3 to contain cells that array2 did not have, and array 1 did, then add else inside foreach

else
{
    $array3[$k] = array($v, null);
}

      

This is done so that the array is evenly shaped as you walk through it.

This will look through each element of array1, check array 2 for each key in array 1 if it exists, adding a new array to this key containing the previous two array data for that key.

+6


source







All Articles