· 1 min read

How to Flatten and Merge Arrays in PHP

There is no built in function for taking a multi dimensional array in PHP and flattening it into a single array. However, it's pretty easy to accomplish in a one liner. Here is the code

<?php
    $a = [['a', 'b', 'c'], ['1', '2', '3']];
    $b = array_merge(...array_values($a));
    // => ['a', 'b', 'c', '1', '2', '3']

This only works for PHP ~> 7

Comments

Leave a comment