php - add list of key's to an multidimensional array -


i'll try brief, yet clear. example:

$foo['test'] = array(); $foo2('test1','test2','test3','test4'); 

is possible create kind of loop multidimensional array?:

$foo['test']['test1']['test2']['test3']['test4'] = ...; 

you don't know in advance how long $foo2 array be. hope question clear , not stopid ask.

any welcome! in advance!

i can't think of valid use-case this. can references (modified version of this answer)

$foo['test'] = array(); $foo2 = array('test1','test2','test3','test4');  $result = array(); $temp = &$result;  foreach($foo2 $value) {     $temp[$value] = array();     $temp = &$temp[$value]; } unset($temp); $foo['test'] = $result; var_dump($foo); 

demo


a stupid , dumb solution using eval(). this should not used. i'm posting fun of ;)

$foo2 = array('test1','test2','test3','test4'); eval("\$res['".join("']['",$foo2)."']=[];"); $foo['test'] = $res; 

demo


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -