foyleman Posted November 17, 2007 Share Posted November 17, 2007 I am having trouble properly setting a dynamic variable that should be part of an array. basically, I want to set this: ${"one[a]"} = '123'; If I echo the result, it looks good: $one[a] = 123; But when I check $GLOBALS, I see: [one[a]] => 123 I worked out an example in case someone can help. $testar = array( 'one' => array( 'a' => '123', 'b' => '456' ) ); print_r($GLOBALS); dec_array($testar); print_r($GLOBALS); function dec_array($arr, $kroot='') { foreach($arr as $key => $val) { if(!empty($kroot)) { $key = $kroot."[".$key."]"; } if(is_array($val)) { dec_array($val, $key); } else { global ${$key}; ${$key} = $val; } } return; } What I want to see is this: [one] => Array ( [a] => 123 => 456 ) But what I actually see is this: [one[a]] => 123 [one] => 456 Link to comment https://forums.phpfreaks.com/topic/77765-dynamic-multidimensional-arrays/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.