Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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%';
share|improve this question
    
What have you tried? –  Dogbert Mar 9 '12 at 14:49
    
I don't understand why you're trying to rewrite the PHP syntax. –  Catfish Mar 9 '12 at 14:50
    
I'm writing a markup language and chose to parse it with PHP. –  Bryan Potts Mar 9 '12 at 15:02
add comment

4 Answers

up vote 4 down vote accepted

Try this number...

function assignArrayByPath(&$arr, $path, $value) {
    $keys = explode('.', $path);

    while ($key = array_shift($keys)) {
        $arr = &$arr[$key];
    }

    $arr = $value;
}

CodePad.

It will loop through the keys (delimited with .) to get to the final property, and then do assignment on the value.

If some of the keys aren't present, they're created.

CodePad.

share|improve this answer
    
This works flawlessly. Looks like the optimal solution as well. Thanks a lot! –  Bryan Potts Mar 9 '12 at 15:19
    
@BryanPotts Optimal enough to be unaccepted... :( ;) –  alex Mar 15 '12 at 11:03
    
for the record, this would be the complementary variant: stackoverflow.com/a/10424516/1388892 –  Adrian Föder Mar 17 at 10:32
add comment

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 this

$confArray = parse_ini_file("filename.ini"); 
var_dump($confArray);
share|improve this answer
    
Not quite. I need the multi-dimensional index, which the ini parser does not do. –  Bryan Potts Mar 9 '12 at 15:08
add comment

Although pasrse_ini_file() can also bring out multidimensional array, I will present a different solution. Zend_Config_Ini()

$conf = new Zend_COnfig_Ini("path/to/file.ini");
echo $conf -> one -> two -> three; // This is how easy it is to do so
//prints one.two.three
share|improve this answer
    
This is a great idea, thanks. –  Bryan Potts Mar 14 '12 at 19:22
add comment

Quick and dirty...

<?php

$input = 'one.two.three = four';

list($key, $value) = explode('=', $input);
foreach (explode('.', $key) as $keyName) {
    if (false === isset($source)) {
        $source    = array();
        $sourceRef = &$source;
    }
    $keyName = trim($keyName);
    $sourceRef  = &$sourceRef[$keyName];
}
$sourceRef = $value;
unset($sourceRef);
var_dump($source);
share|improve this answer
1  
This might be the worst code I've seen in my life. –  Bryan Potts Mar 9 '12 at 15:21
    
Agreed, but "quick and dirty" was the tagline ;) –  Lloyd Watkin Mar 9 '12 at 15:23
    
It's funny because I came here to learn good coding, but now I've seen some of the horrible things you can do (like explode() as), and I'm afraid I'll start using them. lol –  Bryan Potts Mar 9 '12 at 15:27
    
This a good alternative solution. However, it can only assign a string. –  alex Mar 9 '12 at 15:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.