Skip to content

Instantly share code, notes, and snippets.

@abler98
Created April 2, 2016 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abler98/ec0a379d55e5ceb9e406913ec4e1559b to your computer and use it in GitHub Desktop.
Save abler98/ec0a379d55e5ceb9e406913ec4e1559b to your computer and use it in GitHub Desktop.
PHP join_array function
<?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