Created
August 20, 2019 07:21
-
-
Save woprrr/592bdbb6393bbfe0195ec84260f98185 to your computer and use it in GitHub Desktop.
flatten array tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatten($items) | |
{ | |
$result = []; | |
foreach ($items as $item) { | |
if (is_array($item)) { | |
$result = array_merge($result, array_values($item)); | |
continue; | |
} | |
$result[] = $item; | |
} | |
return $result; | |
} | |
$item = [1, 2, 3, [4], 5]; | |
var_dump(flatten($item)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment