AD1ctive Posted December 13, 2009 Share Posted December 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/ Share on other sites More sharing options...
Alex Posted December 13, 2009 Share Posted December 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976541 Share on other sites More sharing options...
AD1ctive Posted December 13, 2009 Author Share Posted December 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976543 Share on other sites More sharing options...
Alex Posted December 13, 2009 Share Posted December 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976544 Share on other sites More sharing options...
AD1ctive Posted December 13, 2009 Author Share Posted December 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976547 Share on other sites More sharing options...
premiso Posted December 13, 2009 Share Posted December 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976576 Share on other sites More sharing options...
AD1ctive Posted December 13, 2009 Author Share Posted December 13, 2009 Cheers now it works!!! I've tried eval() before but not the way you sugessted. It shoudlnt be too dangerous as well as all the data comes from the database and not from $_POST etc. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976582 Share on other sites More sharing options...
PFMaBiSmAd Posted December 13, 2009 Share Posted December 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976584 Share on other sites More sharing options...
AD1ctive Posted December 13, 2009 Author Share Posted December 13, 2009 Yeah but I didnt use an array there The array was in the variable that was giving the name to the variable variable which makes a difference Quote Link to comment https://forums.phpfreaks.com/topic/184989-variable-arrays/#findComment-976588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.