Thursday, 30 August 2018

merge two tables, while holding the numeric key

I am 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."

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

0 comments:

Post a Comment