anon
anon

Reputation:

merging two arrays and keep duplicate values

Is there a php function to merge 2 arrays and keep duplicates?

Like:

$a=array('a','b','c');
$b=array('b','c','b');
array_merge2($a,$b);
//result: array('a','b','c','b','c','b');

thanks

add1:

what the... i previously tested array_merge and it didn't keep duplicate values =/

Upvotes: 3

Views: 6008

Answers (2)

Avilo
Avilo

Reputation: 1204

In the examples for array_splice it shows how to add the contents of one array onto the end of another. Just replace the last parameter with your array.

array_splice($input, count($input), 0, array($x, $y));

So in your example:

$a=array('a','b','c');
$b=array('b','c','b');
array_splice($a, count($a), 0, $b);

Upvotes: 2

Dogbert
Dogbert

Reputation: 222118

array_merge keeps the duplicates.

http://codepad.org/XGcMAi3z

Upvotes: 0

Related Questions