0

I want to be able to pass a string ("['parent_array']['child_array']") to a function that then pulls this string and "my_array" to the front of it and then creates a variable variable.

Then inside the function i do print_r($$string) and nothing comes out. See the code below for a better explanation.

// DOES NOT WORK
$string1 = "my_array['parent_array']['child_array']";
print_r($$string1); //prints nothing.

// WORKS
$string2 = "test";
$test = "This will be printed!";
print_r($$string2); //prints "This will be printed!

// WORKS
print_r($my_array['parent_array']['child_array']);
halliewuud
  • 2,735
  • 6
  • 30
  • 41
  • 2
    May I ask why are you building the variables this way? I could bet a large sum of money for there being a more reasonable approach than *(shudder)* variable variables. – JJJ Mar 28 '12 at 08:51
  • 2
    and / or eval, I bet one of the answers later would actually recommend to use eval – Andreas Wong Mar 28 '12 at 08:52
  • 2
    @andreas No sooner said than done... – JJJ Mar 28 '12 at 08:54
  • Read this http://stackoverflow.com/questions/2036547/variable-variables-pointing-to-arrays-or-nested-objects – Pete Mar 28 '12 at 09:13
  • 2
    You seem to be **asking for a bugfix of a patch from solution to a problem**. That not the way to go. Instead you should describe the problem, show how you tried to sole it and ask for better solution or changes for the existing one. – tereško Mar 28 '12 at 09:54

1 Answers1

0

Variable variables are an ugly, ugly hack.

Use eval.

Example:

$expr = "\$my_array['parent_array']['child_array']";
$val = 'not assigned';
eval("\$val = $expr;");
print_r($val);

But please follow their warnings about the danger of evaling user-supplied code.

Updated: with example

David-SkyMesh
  • 5,041
  • 1
  • 31
  • 38
  • Can I get a example how this would look like? – halliewuud Mar 28 '12 at 08:59
  • 2
    Both are ugly hacks IMHO. Almost always there is a better solution. The real question to OP would be: why do you think you need it? – PeeHaa Mar 28 '12 at 09:40
  • @RepWhoringPeeHaa look here: http://pastebin.com/bBS4iiMC I want to be able to pass a variable to the function that replaces "line29. $arrXml['Assignments']['Assignment']" – halliewuud Mar 28 '12 at 09:49
  • 1
    If `eval()` is the answer, you are asking the wrong question (or have an extreme lisp). – tereško Mar 28 '12 at 09:51
  • 1
    @halliewuud And you can't do `$arrXml[ $var1 ][ $var2 ]` because...? – JJJ Mar 28 '12 at 09:54
  • @halliewuud you know you have a function in a function right? If you call the 'mother function' multiple times you end up with a `fatal error`, because you are trying to redeclare a function. – PeeHaa Mar 28 '12 at 09:55
  • @halliewuud I still don't see why you need either `eval()` or variable variables. See Juhana's comment. – PeeHaa Mar 28 '12 at 09:58
  • You might need to explain yourself better. How does the naming/scoping of an 'inner' function relate to your original question? – David-SkyMesh Mar 28 '12 at 09:58