How to flatten a multi-dimensional array to simple one in PHP?

Spread the love

Sometimes, we want to flatten a multi-dimensional array to simple one in PHP.

In this article, we’ll look at how to flatten a multi-dimensional array to simple one in PHP.

How to flatten a multi-dimensional array to simple one in PHP?

To flatten a multi-dimensional array to simple one in PHP, we call the array_merge function.

For instance, we write

$notFlat = [[1, 2], [3, 4]];
$flat = array_merge(...$notFlat);
var_dump($flat);

to call array_merge with the arrays in the $notFlat array as arguments.

Then $flat is an array with the entries in each nested array.

Conclusion

To flatten a multi-dimensional array to simple one in PHP, we call the array_merge function.

Leave a Reply

Your email address will not be published.