This question already has an answer here:
Just as the title implies, I am trying to create a parser and trying to find the optimal solution to convert something from dot namespace into a multidimensional array such that
s1.t1.column.1 = size:33%
would be the same as
$source['s1']['t1']['column']['1'] = 'size:33%';
Try this number...
CodePad
It will loop through the keys (delimited with
.
by default) to get to the final property, and then do assignment on the value.If some of the keys aren't present, they're created.
I found a solution that worked for me at: Convert Flat PHP Array to Nested Array based on Array Keys and since I had an array based on an .ini file with different keys I made a tiny modification of that script and made work for me.
My array looked like this:
On request, this is the code that I described was working for me:
This is the source of the .ini file read by the parse_ini_file():
This is the outcome of the code above:
Although pasrse_ini_file() can also bring out multidimensional array, I will present a different solution. Zend_Config_Ini()
I am pretty sure you are trying to do this to store some configuration data or similar.
I highly suggest you to save such file as
.ini
and use parse_ini_file() function to change the configuration data into a multidimensional array. As simple as thisI would suggest using dflydev/dot-access-data.
If you're not familiar with using Composer, head over to https://getcomposer.org/ for an introduction so that you can download and autoload the package as as dependency for your project.
Once you have the package, you can load a multi-dimensional array into a Data object:
And access the values using dot notation:
Quick and dirty...