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
https://forums.phpfreaks.com/topic/184989-variable-arrays/
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
https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976541
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
https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976543
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
https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976544
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
https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976547
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
https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976584
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.