Created
May 30, 2014 18:06
-
-
Save jasonrhodes/ad8a6922e05c4be30b2f to your computer and use it in GitHub Desktop.
Take a list of strings and turn it into a nested associative 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 | |
function nestify($list, &$master) { | |
if (empty($list)) { | |
return $master; | |
} | |
$key = array_shift($list); | |
if (empty($master[$key])) { | |
$master[$key] = array(); | |
} | |
nestify($list, $master[$key]); | |
} | |
$meta = array("region" => array("blah" => "duh")); | |
$list = ["region", "hero", "thing"]; | |
nestify($list, $meta); | |
print_r($meta); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is this example code executed in a PHP sandbox: http://sandbox.onlinephpfunctions.com/code/b78695ef2300cd8c8648f75ea659dbd79d3dd15e