Created
January 8, 2019 11:54
-
-
Save dethell/153f31aed84cd8f5150903c275fd3529 to your computer and use it in GitHub Desktop.
PHP Flatten Arrays
This file contains 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
<?php | |
$array = [[1,2,[3]],4]; | |
function returnValues($someArray, &$newArray) { | |
foreach ($someArray as $value) { | |
echo "checking value: " . print_r($value, true) . "<br/>\n"; | |
if (is_array($value)) { | |
$values = returnValues($value, $newArray); | |
} | |
else { | |
echo "pushing value<br/>\n"; | |
$newArray[] = $value; | |
} | |
} | |
} | |
$newArray = []; | |
returnValues($array, $newArray); | |
print_r($newArray); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment