Organizational Research By

Surprising Reserch Topic

how to recursively create a multidimensional array using -'php,recursion,multidimensional-array'


how to recursively create a multidimensional array  using -'php,recursion,multidimensional-array'

I am trying to create a multi-dimensional array whose parts are determined by a string. I'm using . as the delimiter, and each part (except for the last) should be an array
ex:

config.debug.router.strictMode = true


I want the same results as if I were to type:

$arr = array('config' => array('debug' => array('router' => array('strictMode' => true))));


This problem's really got me going in circles, any help is appreciated. Thanks!
    

asked Sep 8, 2015 by rajesh
0 votes
8 views



Related Hot Questions

5 Answers

0 votes
I say split everything up, start with the value, and work backwards from there, each time through, wrapping what you have inside another array. Like so: $s = 'config.debug.router.strictMode = true'; list($parts, $value) = explode(' = ', $s); $parts = explode('.', $parts); while($parts) { $value = array(array_pop($parts) => $value); } print_r($parts); Definitely rewrite it so it has error checking.
answered Sep 8, 2015 by rajesh
0 votes
Let√Ę
answered Sep 8, 2015 by rajesh
0 votes
Gumbo's answer looks good. However, it looks like you want to parse a typical .ini file. Consider using library code instead of rolling your own. For instance, Zend_Config handles this kind of thing nicely.
answered Sep 8, 2015 by rajesh
0 votes
I really like JasonWolf answer to this. As to the possible errors: yes, but he supplied a great idea, now it is up to the reader to make it bullet proof. My need was a bit more basic: from a delimited list, create a MD array. I slightly modified his code to give me just that. This version will give you an array with or without a define string or even a string without the delimiter. I hope someone can make this even better. $parts = "config.debug.router.strictMode"; $parts = explode(".", $parts); $value = null; while($parts) { $value = array(array_pop($parts) => $value); } print_r($value);
answered Sep 8, 2015 by rajesh
0 votes
// The attribute to the right of the equals sign $rightOfEquals = true; $leftOfEquals = "config.debug.router.strictMode"; // Array of identifiers $identifiers = explode(".", $leftOfEquals); // How many 'identifiers' we have $numIdentifiers = count($identifiers); // Iterate through each identifier backwards // We do this backwards because we want the "innermost" array element // to be defined first. for ($i = ($numIdentifiers - 1); $i >=0; $i--) { // If we are looking at the "last" identifier, then we know what its // value is. It is the thing directly to the right of the equals sign. if ($i == ($numIdentifiers - 1)) { $a = array($identifiers[$i] => $rightOfEquals); } // Otherwise, we recursively append our new attribute to the beginning of the array. else { $a = array($identifiers[$i] => $a); } } print_r($a);
answered Sep 8, 2015 by rajesh

...