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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.