Array combine help required

Hi everyone,

I have two arrays which I need to merge into one array. The first array’s values need to become the keys, the second array’s values stay the same.


array (size=3)
  0 => string '6' (length=1)
  1 => string '6' (length=1)
  2 => string '5' (length=1)


array (size=3)
  0 => string '0' (length=1)
  1 => string '1' (length=1)
  2 => string '0' (length=1)

After using the array_combine function I end up with:

array (size=2)
  6 => string '1' (length=1)
  5 => string '0' (length=1)

What I actually want is:

  6 => string '0' (length=1)
  6 => string '1' (length=1)
  5 => string '0' (length=1)

Can anyone please help me out?


Hi RedBishop,

It’s not going to be possible to do that, as you can’t have an array with duplicate keys.

What you could do is create a multidimensional array to hold the values, that would look like this:

    0 => array('id' => 6, 'value' => 0),
    1 => array('id' => 6, 'value' => 1),
    2 => array('id' => 5, 'value' => 0),

Hey fretburner,

Makes sense of course, don’t know what I was thinking. Do you perchance know of any technique to compare 2 multidimensional arrays? I need to compare the arrays and then do something with those key/value pairs that aren’t in both arrays. Been trying various things all day long.

This is what one of those arrays would look like:

array (size=3)
  0 => 
    array (size=2)
      'id' => int 6
      'value' => int 0
  1 => 
    array (size=2)
      'id' => int 6
      'value' => int 1
  2 => 
    array (size=2)
      'id' => int 5
      'value' => int 0

Thanks for your help and I hope you’re well by the way.

$out = array();
foreach ($array1 as $i => $v)
    $out[] = array('id' => $v, 'value' => $array2[$i]);

Define ‘compare’. What’re you trying to get out of it? How are you defining what key/value pairs aren’t in both arrays? If i have id = 6 in both arrays but different values, is it a match or not? What about multiple values for the same ID?

Hi ScallioXTX and StarLion,

thank you for helping me with this.

@rpkamp, thanks but I receive some kind of offset error.

Array1 might have id6 value0, id6 value1, and id5 value0. Array2 might only have id5 value0. The array I wish to create would contain id6 value0 and id6 value1, both of which are missing from Array2.


So you want to array_diff a multidimensional array.
Specifically, you want to array_udiff.

function comp_array($a,$b) {
  return ($a['id'] == $b['id'] && $a['value'] == $b['value']) ? 0 : 1;
$adiff = array_udiff($array1,$array2,'comp_array'); //Find anything in array1 that isnt in array2
$bdiff = array_udiff($array2,$array1,'comp_array'); //Vice-versa
$diff = array_merge($adiff,$bdiff);

Hi StarLion,

please excuse my delayed response, I haven’t had an opportunity to test the code. Thank you for posting the function. I did try it but haven’t managed to get it working. It either returns an empty array or it returns all of the contents from one of the arrays, i.e., the same array.

Will try again later, perhaps I did something wrong. Thanks!