Skip to content

Instantly share code, notes, and snippets.

@dethell
Created January 8, 2019 11:54
Show Gist options
  • Save dethell/153f31aed84cd8f5150903c275fd3529 to your computer and use it in GitHub Desktop.
Save dethell/153f31aed84cd8f5150903c275fd3529 to your computer and use it in GitHub Desktop.
PHP Flatten Arrays
<?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