Created
April 2, 2016 16:50
-
-
Save abler98/ec0a379d55e5ceb9e406913ec4e1559b to your computer and use it in GitHub Desktop.
PHP join_array function
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 | |
function join_array(array $array, $divider = '.') { | |
$iterator = new RecursiveArrayIterator($array); | |
$new_array = []; | |
$recursive = function (RecursiveArrayIterator $iterator, $parent = null) use (&$recursive, &$new_array, $divider) { | |
while ($iterator->valid()) { | |
$path = !is_null($parent) ? $parent . $divider . $iterator->key() : $iterator->key(); | |
if ($iterator->hasChildren()) { | |
$recursive($iterator->getChildren(), $path); | |
} else { | |
$new_array[$path] = $iterator->current(); | |
} | |
$iterator->next(); | |
} | |
}; $recursive($iterator); | |
return $new_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment