Dynamic Multi-Dem Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Dynamic Multi-Dem Array

Post by hawleyjr »

I'm pretty sure I've done this before...I'm just not getting it to work as I want.

I have an array:

Code: Select all

<?php
$aTest = array('level1','level2','level3','level4');
With the values of the array. I would like to create a multi-dem array such as:

Code: Select all

<?php
$aTest2['level1']['level2']['level3']['level4'] = array();
?>
Any ideas?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Like this?

Code: Select all

$aTest = array('level1' => array('level2' => array('level3' => array('level4' => array()))));
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

TheMoose wrote:Like this?

Code: Select all

$aTest = array('level1' => array('level2' => array('level3' => array('level4' => array()))));
That would produce what I'm trying to do however, it doesn't answer the question of how do I go from :

Code: Select all

$aTest = array('level1','level2','level3','level4');
To:

Code: Select all

$aTest2['level1']['level2']['level3']['level4'] = array();
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Not the most secure, but it works:

Code: Select all

$aTest = array('level1', 'level2', 'level3', 'level4');
$aTest2 = array();
$str = "$" . "aTest2";
for($i=0;$i<count($aTest);$i++) {
	$str .= "['" . $aTest[$i] . "']";
}
$str .= " = array();";
eval($str);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You may be onto something using eval() however, it still doesn't produce the desired result...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Thanks Feyd, I can work off of that :)

Code: Select all

$in = array('level1', 'level2', 'level3', 'level4','');

function storeInto($a) {
        $aLast = count($a) - 1;
        $result = array($a[$aLast]);
        for($i=$aLast-1;$i>=0;$i--) {
                $result = array($a[$i] => $result);
        }
        return $result; 
}

$out = storeInto($in);
print_r($out);
Will produce:

Code: Select all

Array
(
    [level1] => Array
        (
            [level2] => Array
                (
                    [level3] => Array
                        (
                            [level4] => Array
                                (
                                    [0] => 
                                )

                        )

                )

        )

)
Post Reply