0

In php there is variable variable names:

As the example goes:

$a = 'hello';
$$a = 'world';

echo $hello; // outputs world

Now, we also have nested arrays:

$myvar = Array(
    'a' => Array('b' => 1, 'c' => 2),
    'd' => Array('e' => 3, 'f' => 4)
);

echo $myvar['a']['e']; // outputs e

Question: is it possible to access such array with variable names?

Something like the following:

$myvarname = 'myvar[a][e]';
echo $$myvarname;

If yes - how?

EDIT:

What I am trying to do is build an array in a loop. I have no idea about the incoming data, so array can be any level of depth.

There is a number of input rows formatted like this:

 /katalog/category1/subcategory1/subcategory2

I made a loop through input lines, and split the URLs with "/".

After that I am trying to build an array, which will represent the structure of all urls.

 category 1
 --> subcategory 1
     --> subcategory 2
     --> subcategory 3
         --> subcategory 4
 category 2
 --> subcategory 5
 category 3
 category 4

The main part of the question is how to use variable variable for nested array. If at all possible. I included the actual use case because it was asked from me how that can be used in real life. Don't need a solution to my problem - I can solve that question myself.

However, variable variable usage - that is the real question.

1
  • Recursion is your friend. I'm gonna try to whip up a quick answer. Commented Apr 16, 2014 at 15:25

2 Answers 2

1

It could be done with eval():

$myvarname = "myvar['a']['b']";
eval("\$result = \${$myvarname};");
var_dump($result);

Output:

int(1)

Demo


But using eval() is generally a bad idea, especially if this involves user input. The function allows arbitrary code to be executed - which means a user with malicious intent could execute harmful code on your server. This is just a POC and should not be used in production code.

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

7 Comments

I added what I am trying to do. Sorry, but I dont think eval will help me =(
@Toly: Can you show a sample input string and the expected array output? (The print_r() output of it)
Hmm... just a catalog page. Starts with /catalog/ and has ([a-zA-Z0-9]*) multiple times, how many times - not known. Expected output - like a directory. I am just trying to parse many urls and see what is the structure. But the actual task is not that much important. Variable variable for nested array is more interesting for me in this question - I will solve my problem with some time.
@Toly: Variable variables works basically the same as eval(). I'm not sure why you're specifically trying to access them using variable variables? I don't think it fits the use-case here.
I understand that. I was just wondering if directly using that syntax is possible for arrays or not.
|
1

I suggest using recursion to solve this problem. I wasn't 100% sure on the structure of the output, but hopefully this should be able to point you in the right direction:

function createArray($pieces, $array){
  if(count($pieces) > 1){
    $key = array_shift($pieces);
    $array[$key] = createArray($pieces, isset($array[$key]) ? $array[$key] : []);
  }
  else{
    $array[$pieces[0]] = [];
  }

  return $array;
}

DEMO: https://eval.in/137546

1 Comment

+1 for a nice alternative solution ;). I am not taking that answer as accepted because I was mainly wondering if variable variable names can be used to access the nested array.

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.