DEV Community

Jimmy Klein
Jimmy Klein

Posted on Edited on

Never use array_merge in a loop in PHP

I often see people using array_merge function in a for/foreach/while loop 😱 like this :

$arraysToMerge = [ [1, 2], [2, 3], [5, 8] ];

$arraysMerged = [];
foreach($arraysToMerge as $array) {
    $arraysMerged = array_merge($arraysMerged, $array);
}
Enter fullscreen mode Exit fullscreen mode

It's a very bad practice because it's a performance killer (especially in memory).

Since PHP 5.6, there is a new operator : the spread operator

$arraysToMerge = [ [1, 2], [2, 3], [5,8] ];

$arraysMerged = array_merge([], ...$arraysToMerge);
Enter fullscreen mode Exit fullscreen mode
  • No more performance problem
  • BONUS : no more for/foreach/while loop
  • BONUS : process in one line

Look now at your code base to find code that you can improve 👩‍💻👨‍💻!


Thank you for reading, and let's stay in touch !

If you liked this article, please share. You can also find me on Twitter/X for more PHP tips.

Top comments (10)

Collapse
 
attkinsonjakob profile image
Jakob Attkinson •

This solution is pretty cool, however if you have an array of objects this solution won't work anymore.

Say you have a list of users and each use has multiple social media accounts.

How would you create an array that has all the social media accounts from all the users? I can't find a better solution than this...

$accounts = [];
foreach ($users as $user) {
    $accounts = array_merge($accounts, $user['socialMediaAccounts']);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
klnjmm profile image
Jimmy Klein • • Edited

In this case, you have to do a intermediate process

$accounts = [];
foreach ($users as $user) {
    $accounts[] = $user['socialMediaAccounts'];
}

$accounts = array_merge([], ...$accounts);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jfernancordova profile image
José Córdova • • Edited

A fancy way:

$accounts = array_map(static function($user){
    return $user['socialMediaAccounts'];
}, $users);

$accounts = array_merge([], ...$accounts);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
einenlum profile image
Yann Rabiller •

Hi :)

I think you meant in PHP 7.4? wiki.php.net/rfc/spread_operator_f...

Collapse
 
klnjmm profile image
Jimmy Klein •

Hi,

No, this work in PHP 5.6 : php.net/manual/en/migration56.new-..., "Argument unpacking via ..." section

Collapse
 
einenlum profile image
Yann Rabiller •

Oh! Indeed, array_merge is a function so variadic arguments work… Did not think about it . Thx!

Collapse
 
nguyenhai97ict profile image
Nguyễn Đăng Hải •

So basically different way of using array_merge so it still array_merge after all :?

Collapse
 
klnjmm profile image
Jimmy Klein •

Sorry but I don't understand your comment...

Collapse
 
chris_daniel profile image
Christopher Daniel • • Edited

I think if we pass more than two arrays as arguments, it will call k way merge. It is faster than array_merge in loop

Collapse
 
ohvitorino profile image
Bruno Vitorino •

Hi! Nice article.
Could you maybe also add some performance stats comparing both approaches?