3

I'd like to combine the two arrays below into one. More specifically, I want to add the contents of the second array to the the matching key in the first array. The keys in the final array should have contents of 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 it doesn't combine/merge the arrays correctly be matching keys.

I also tried it with array_combine but since it creates an array by using one array for keys and another for its values I couldn't get that to work either.

hakre
  • 193,403
  • 52
  • 435
  • 836
Georg
  • 610
  • 2
  • 8
  • 22
  • can you please provide sample output array here ? –  Mar 12 '12 at 23:26
  • what are the conditions? it seems like you are operating on time here. what happens if i add and it exceeds `23:59`? are both arrays always equal or is there any chance one array has more items than the other? do both arrays contain the same items, or can there be an item that is in one array and not the other? – Joseph Mar 12 '12 at 23:27
  • Hi @Teez, the array_merge outputs one array that is a simple combination of both. The foreach approach outputs one array with empty values and no values from arr1. array_combine gives the "Both parameters should have an equal number of elements in..." error. – Georg Mar 12 '12 at 23:35
  • Hi @Joseph, both arrays have almost always an unequal number of keys. That's why I'd like to combine the values into on array. Array one always has all keys but array two has some keys that are not in array two, hence all the values of matching keys should be in the final array. – Georg Mar 12 '12 at 23:38

2 Answers2

7

Okay, so I have two arrays of data and an empty one to hold the new array

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

To combine them based on key

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

$array3 holds the new array. If you want to overwrite the original array just add

$array1 = $array3;

And if you'd like for array3 to hold the cells that array2 didnt have and array 1 did have then add an else inside the foreach

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

This is to make it so the array is uniformly shaped when you loop through it.

That will look through every element of array1, check array 2 for each key in array 1, if it exists, adding a new array at that key holding the previous two array's data for that key.

Jake
  • 4,134
  • 2
  • 16
  • 20
  • Thank you, I just came across this and tweaked it a bit for my purpose, but your explanation got me over a big hump in the project I am working on. Thanks! – kjones Jun 05 '12 at 19:20
0

In additional to @Jake's answer if you want to include key and values for missing items.

 $attributes = array();

    if (count($columns) != count($data)) {
        foreach ($columns as $key => $value) {
            array_key_exists($key, $data) ? 
           $attributes[$value] =  $data[$key] 
           : $attributes[$value] =  "";
        }
    } else {
    //Else if arrays are equal combine using `array_combine` function
        $attributes = array_combine($columns, $data);
    }

    return  $attributes;
Theodory
  • 301
  • 1
  • 3
  • 12