Merging arrays in a loop is slow and causes high CPU usage.
Typical example when array_merge
is being used in the loop:
$options = [];
foreach ($configurationSources as $source) {
// code here
$options = array_merge($options, $source->getOptions());
}
In order to reduce execution time array_merge
can be called only once:
$options = [[]];
foreach ($configurationSources as $source) {
// code here
$options[] = $source->getOptions();
}
// PHP 5.6+
$options = array_merge(...$options);