Jump to content

Variable arrays


AD1ctive

Recommended Posts

Hey!

 

I've got another problem now: Php supports variable variables ( http://php.net/manual/en/language.variables.variable.php ) like the following example:

 

function setVar($name, $value) {
    $$name = $value;
}

 

Now I'd like to set an array using the same method:

 

function setArray($name, $value) {
    $$name = $value;
}

$name = "test[0][1][4]";
setArray($name, "test");

 

Unfortunately, this doesnt't work. Do u have an idea?!

 

Thanks for ur help ;)

Link to comment
Share on other sites

This cannot be done because variable names must be strings, they can't be arrays, that just doesn't make any sense. That's like trying to do this:

 

$arr = array('something');
$$arr = 'something';
// Is the same as..

$array('something') = 'something';

 

Which makes no sense at all.

Link to comment
Share on other sites

Im not sure if i really got u but what I wanted to create is something like this:

 

$array[0][1][1] = "value";

 

So thats why i thought this is goin to work:

 

$name = "array[0][1][1]";
$$name = "value";

 

What I was hopin for is that example one is the same as example 2.

 

Thanks again ;)

Link to comment
Share on other sites

Well just doing $name = "array[0][1][1]"; doesn't make $name an array, it's just a string. This might be totally in the opposite direction from what you're looking for, but if you want to have a variable that you can edit that will also edit $array[0][1][1] you can pass that by reference. Example:

 

$array[0][1][1] = 'value';
$name = &$array[0][1][1];
$name = 'Something';
echo $array[0][1][1]; // Something

Link to comment
Share on other sites

The problem is that the part [0][1][1] is created by a script and can always be different. But it actually kind of works - i just figured out that

 

$name = "testArr[0][0][0]";
$$name = "test";

 

creates an array. Unfortunately it looks like this:

 

testArr => Array([0][0][0] => "test");

 

and not like it should be:

 

testArr => Array([0] => Array([0] => Array ([0] => "test" )));

 

Maybe u can get the whole idea now?! Thanks ;)

Link to comment
Share on other sites

This way can be dangerous so filter vigoursly and use with caution:

 

<?php
$string = '$newArr[0][0][1][3] = "test";';
eval($string);

print_r($newArr);
?>

 

That is the only way it is possible to do what you want without a recursive function.

Link to comment
Share on other sites

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
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.