2

Everybody knows that you can access a variable in PHP using this: ${'varName'}. But when you need to get/set a variable witch is part of an array, why doesn't it work ? Suppose we have this piece of code:

<?php
   $myArray = array(...);
   $myVarName = "myArray['my1']['my11']['my111']";
   ${$myVarName} = "new value";
?>

Shouldn't it work ? I have tested it again and again - it is not working.. Is it there a way to do that?

4
  • 2
    Those are called variable variables. You should never ever use them. What is the underlying problem you're trying to solve? Commented Oct 21, 2011 at 14:23
  • I work project based for someone who is not using any framework and i need to build some functions to help myself. Therefor I try to build a particular function that should get a list of categories and create a tree (parent->child) for unlimited levels. I get to some point (in a recursive function from witch i have to set some array vars in the ordered array). Commented Oct 21, 2011 at 14:34
  • .. and I have to build the path to the array var that i need to set. Commented Oct 21, 2011 at 14:37
  • 4
    Why should you "never ever use" a variable variable? They can often be very useful and if used correctly there's nothing wrong with them. Commented Apr 19, 2013 at 2:49

3 Answers 3

12

I recommend you not to use dynamic variables like ${$var}. What you want is modifying a multi-dimensional associative array according to a path of keys.

<?php
$myArray = array(...); // multi-dimensional array
$myVarPath = array('my1', 'my11', 'my111');
setValueFromPath($myArray, $myVarPath);

function getValueFromPath($arr, $path)
{
    // todo: add checks on $path
    $dest = $arr;
    $finalKey = array_pop($path);
    foreach ($path as $key) {
        $dest = $dest[$key];
    }
    return $dest[$finalKey];
}

function setValueFromPath(&$arr, $path, $value)
{
    // we need references as we will modify the first parameter
    $dest = &$arr;
    $finalKey = array_pop($path);
    foreach ($path as $key) {
        $dest = &$dest[$key];
    }
    $dest[$finalKey] = $value;
}

This is a procedural example to keep it simple. You may want to put your hierarchical array and this functions inside a class.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, Mytskine! That is what i need! [ +10 ] (having 1 reputations, I can not vote..)
@Macovei.Radu Glad I could help. It was a simple, but interesting question (I seldom use references in PHP). You can still accept the answer by clicking on the check mark at the left of the answer (see the SO FAQ for more).
1

Shouldn't it work ?

No.

Everybody knows that you can access a variable in PHP using this: ${'varName'}.

Yes. Yet everybody knows that's lame.

How to refer dynamically to a php array variable(s)?

having array of ('my1','my11','my111') you can refer to any particular array member using merely a loop.

4 Comments

I agree dynamic variables are lame. But I believe you're wrong when you push aside the question by "using merely a loop". Modifying a multi-dimensional associative array according to a path of keys isn't that obvious. Won't it at least need references?
@Mytskine first I'd ask if you really need that
@Col. Shrapnel Do you have any suggestion for creating a tree (parent->child) array based from a simple query result? This is what I am trying to do. People who hired me for the job I work on are not using any frameork - therefor I do not have prerogatives that this kind of tools deliver.
@Col.Shrapnel Maybe he doesn't need this, but that's what he asked for, and it seems a legitimate question to me. If you have doubt on his needs, then tell him about alternatives. I've posted my own answer, and I believe it's a bit more constructive.
0

You could do something like this, but it would be a really bad idea. Sorry Col. ;)

<?php
$mA = array();
$mVN = "mA['m1']['m2']['m3']";
eval('$'. $mVN . ' = "new value";');
print_r($mA);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.