Jump to content

dynamic multidimensional arrays


foyleman

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.