Last active
October 5, 2015 22:03
-
-
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)
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 | |
/** | |
* 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