30 seconds of PHP

  Back to Home

deepFlatten

arrayrecursionintermediate

Deep flattens an array.

Use recursion. Use array_push, splat operator and an empty array to flatten the array. Recursively flatten each element that is an array.

function deepFlatten($items)
{
  $result = [];
  foreach ($items as $item) {
    if (!is_array($item)) {
      $result[] = $item;
    } else {
      array_push($result, ...deepFlatten($item));
    }
  }

  return $result;
}