Skip to content

Instantly share code, notes, and snippets.

@nufaylr
Forked from SeanCannon/array_flatten.php
Last active August 29, 2015 14:23
Show Gist options
  • Save nufaylr/6ac181a5728ae5bec151 to your computer and use it in GitHub Desktop.
Save nufaylr/6ac181a5728ae5bec151 to your computer and use it in GitHub Desktop.
<?php
/**
* Convert a multi-dimensional array into a single-dimensional array.
* @author Sean Cannon, LitmusBox.com | seanc@litmusbox.com
* @param array $array The multi-dimensional array.
* @return array
*/
function array_flatten($array) {
if (!is_array($array)) {
return false;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result[$key] = $value;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment