Source Code:
(back to article)
<?php function flattenArray($array) { return array_reduce( $array, function ($carry, $item) { if (is_array($item)) { return array_merge($carry, flattenArray($item)); } else { $carry[] = $item; return $carry; } }, [] ); } $multiDimensionalArray = [1, [2, 3], [4, [5, 6]]]; $flattenedArray = flattenArray($multiDimensionalArray); print_r($flattenedArray);
Result:
Report an issue