No Array_merge In Loops
March 31, 2016 ยท View on GitHub
array_merge accepts an arbitrary number of arrays as argument. Calling this function once with all the arguments is faster than calling the same function inside a loop, each time with one argument.
Besides, array_merge particularly slow and memory intensive when it is used in a loop.
array_merge is particularly slow and memory intensive when it is used in a loop.
<?php
$result = array();
foreach ($source as $list) {
$result = array_merge($result, $list);
}
?>
At each iteration, PHP copies the array into another temporary array, leading to as many temporary allocation as there are elements in the original $source array. Each time, the memory allocated will be more than the previous one, accelerating the memory usage.
Beside the memory allocation, the copy of the data from one temporary value to the other will also consume time and processing power.
The solution to this problem is to call array_merge once with all its arguments. In this case, or if the number of arrays to be merged is unknown at development time, using variadic functions or call_user_func_array is also much more efficient.
<?php
//$source is a array of arrays : $source[0] = [ /**/ ];
// Hard-coded version, when the all elements from $source are known
$result = array_merge($source[0], $source[1], $source[2]);
// Dynamic version :
// PHP 5.6 and later
$result = array_merge(...$source);
// PHP 5.5 and before
$result = call_user_func_array('array_merge', $source);
?>
Actually, using call_user_func_array or ... is faster for several other array functions. Note that memory impact, such as explained for array_merge may be lower when the function removes data :
array_merge_recursivearray_mergearray_replace_recursivearray_replacearray_diffarray_diff_assocarray_diff_keyarray_diff_uassocarray_diff_ukeyarray_intersectarray_intersect_assocarray_intersect_keyarray_intersect_uassocarray_intersect_ukeyarray_udiffarray_udiff_assocarray_udiff_uassocarray_uintersectarray_uintersect_assocarray_uintersect_uassocmaxminwddx_serialize_varswddx_add_varsarray_push[1]array_unshift[1]printfprintf
Rule Details
The following snippets are considered a warning:
<?php
foreach($a as $b) {
$c = array_merge($c, $b);
}
for($i = 0; $i < 10; $i++) {
$c = array_merge($c, $array[$i]);
}
while(count($a) > 0) {
$c = array_merge($c, array_pop($a));
}
?>