views:

53

answers:

4

im trying to merge two arrays together. both have numeric keys and are unique. when i use array_merge, it re-indexes starting at 0.

so lets say i have

[2] = abc
[5] = cde

and i have

[32] = fge
[13] = def

i want to merge these two together maintaining the unique keys.

below is the explaination on the current merge behavior.. any way around this?

"If all of the arrays contain only numeric keys, the resulting array is given incrementing keys starting from zero."

+2  A: 

Try the array union operator +.

deceze
+2  A: 

Try using the + operator.

$one = array(2 => 'abc', 5 => 'cde');
$two = array(32 => 'fge', 13 => 'def');
$three = $one + $two;

$three should now look like this:

[2] = abc
[5] = cde
[32] = fge
[13] = def
Matt Huggins
wow. too easy :) thanks!
Roeland
A: 

Try this:

$arr1 = array();
$arr2 = array();
$arrmerge = array();
array_push($arr, $arr1, $arr2);

$arr1 and $arr2 will be merge and stored in $arrmerge. You can access it by foreach.

Hope it works!

Manie
Actually no, this would create a multidimensional array `array($arr1, $arr2)`.
deceze
+1  A: 
$result = array(2 => 'abc', 5 => 'cde') + array(32 => 'fge', 13 => 'def');
print_r($result);
NAVEED