Skip to content

Instantly share code, notes, and snippets.

@b-b3rn4rd
Last active October 5, 2015 22:03
Show Gist options
  • Save b-b3rn4rd/14a1c069f334ba5fe1f8 to your computer and use it in GitHub Desktop.
Save b-b3rn4rd/14a1c069f334ba5fe1f8 to your computer and use it in GitHub Desktop.
Convert single level array that uses "dot" notation into nested array (opposite to array_dot)
<?php
/**
* Do an opposite of what array_dot does
*
* @param array $array
* @param string $delimiter
* @return array "undotted" nested array
*/
function array_undot(array $array, $delimiter = '.')
{
$return = [];
foreach ($array as $path => $value) {
if (stripos($path, $delimiter) === false) {
$return[$path] = $value;
continue;
}
$namespaces = $value;
foreach (array_reverse(explode($delimiter, $path)) as $key) {
$namespaces = [$key => $namespaces];
}
$return = array_merge_recursive($return, $namespaces);
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment