0

Is there a better way to achieve the following:

array_unique(array_merge($array_one, $array_two))

That is, merge two arrays and only have unique values.

array_merge seems to merge uniquely for keys, but i need unique values and can not see a built in function that does this.

I understand 'better' may be subjective. Im thinking purely in the fact that im using two array functions wraped to get the values.

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200

1 Answers1

0

Not sure you can do it in one fail swoop - two functions isn't bad though .. the only other suggestion is to slip in a condition when forming the second array ($array_two - or the last array formed), e.g.

for($i=0; $i<10; $i++) {
   $array_one[] = $i;
}

for($k=5; $k<15; $k++) {
   if(!in_array($k,$array_one)) $array_two[] = $k;
}

Although of course if the first array isn't fully formed (finished) before the second array begins then this won't help. And if it takes too much delving to do this I would consider keeping the original - as long as it does what you want_g