up vote 0 down vote favorite
share [g+] share [fb]

Good evening (middle-european time).

Although my question could be a bit embarassing (because it may be sooo easy) I'm asking you. I apologize in advance but i've been coding for about 10 hours today and need to get this last thing done.

So, is there an array function - honestly i searched for about an hour - in php that somehow does array_merge, comparing the values, ignoring the keys. I think array_unique(array_merge($a, $b)); must be working, however I believe there is a nicer way to do this.

eg.

$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);

resulting in:

$ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4);

Please note that I don't care about the keys in $ab, however it would be nice if they were ascending, starting at 0 to count($ab)-1.

Thanks in advance.

link|improve this question

your solution is fine – mfonda Jan 11 '11 at 17:47
ok mfonda, thank you very much! – Valorized Jan 11 '11 at 17:49
1  
I don't think there is a nicer way to do this. array_unique(array_merge($a, $b)) is actually a pretty elegant solution. – Ben Lee Jan 11 '11 at 17:50
It is good to note that this will only work if the keys are numeric or guaranteed to be unique between the two arrays, otherwise array_merge will overwrite. – Will Jan 11 '11 at 17:52
Thanks Ben Lee. Now I can go to bed ;-) I just thought I got somehow... blind. Thanks to all. – Valorized Jan 11 '11 at 17:53
feedback

4 Answers

up vote 0 down vote accepted
function umerge($arrays){
 $result = array();
 foreach($arrays as $array){
  foreach($array as $value){
   if(array_search($value,$result)===false)$result[]=$value;
  }
 }
 return $result;
}
link|improve this answer
i will select this on as the correct answer, because it is a nice way to do it. However you wrote "reult" instead of result in line 5. Another way: array_merge(array_unique(array_merge($a, $b))); Thanks all. – Valorized Jan 11 '11 at 18:06
thanx I´ll edit it – Oliver A. Jan 11 '11 at 18:12
1  
Or... array_unique(array_merge($myArray,$myOtherArray)); – doublejosh Aug 10 '11 at 1:35
feedback

array_merge will ignore numeric keys, so in your example array_merge($a, $b) would give you $ab, there is no need to call array_unique().

if you have string keys (i.e. an associative array) use array_values() first:

array_merge(array_values($a), array_values($b));
link|improve this answer
he wants the unique values though, so he needs to call array_unique – Will Jan 11 '11 at 17:55
where exactly does it say he only only wants unique values? just because he was using array_unique doesn't mean anything - he freely admitted he wasn't sure what to use, and the description makes no mention of wanting only unique entries. – Stephen Jan 11 '11 at 17:59
@Will, you are right, values must be unique @Stephen: sorry, it might have been confusing. solution is: array_merge(array_unique(array_merge($a, $b))); because the second array_merge sorts the key from 0 to 4! Thanks all! – Valorized Jan 11 '11 at 18:02
in that case, you don't need two array_merge calls - an array_values call is probably a better choice – Stephen Jan 11 '11 at 18:06
look at the example. – Oliver A. Jan 11 '11 at 18:07
feedback

For larger arrays and (to answer the question as asked) for associative arrays, I believe that you will find this solution more satisfactory:

/**
 * array_merge_unique - return an array of unique values,
 * composed of merging one or more argument array(s).
 *
 * As with array_merge, later keys overwrite earlier keys.
 * Unlike array_merge, however, this rule applies equally to
 * numeric keys.
 */
function array_merge_unique(array $array1 /* [, array $...] */) {
  $args = func_get_args();
  $result = array_flip(array_flip($array1));
  for ($i=1;$i<count($args);$i++) {
    $result =
      array_flip(
        array_flip(
          array_merge($result,$args[$i])));
  }
  return $result;
}
link
feedback
$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);

//add any from b to a that do not exist in a
foreach($b as $item){


    if(!in_array($item,$b)){
        $a[] = $item
    }

}

//sort the array
sort($a);
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.