Suppose you have two (numeric) arrays $a
and $b
and want to achieve what you get by doing
$a = & array_merge($a, $b);
efficiently.
Is this already the way to do it? I find array_merge suspicious because it merges associative keys. Also I suppose the array is not updated but copied unnecessarily. Is there a way to call array_push with an array? Or would you do it in a loop with $a[] = ...
assignments?
array_merge
will reindex keys. To maintain keys, use $c = $array1 + $array2
. If $array2
has an existing key, it will be ignored. You can also array_push
, array_pop
, array_shift
, and array_unshift
for item addition and subtraction.