-
-
Save itowhid06/cfec67436b7089b4a7639a8f1ec40a01 to your computer and use it in GitHub Desktop.
PHP array_flatten() function. Convert a multi-dimensional array into a single-dimensional array.
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 | |
/** | |
* 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; | |
} |
Author
itowhid06
commented
Mar 19, 2018
•
function array_flatten($array = null) {
$result = array();
if (!is_array($array)) {
$array = func_get_args();
}
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result = array_merge($result, array($key => $value));
}
}
return $result;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment