Hello everyone,

i have a little problem with variable variables if the variable is an multidimensional array and the indexes are not fix so, they are dynamic.

I need something like this:

$varName = "data";
$varIndex = "[1]['items'][4]['items'][]";

${varName.$varIndex} = 'xyz' ; // this does not work as php looks for a wrong variable, it takes the whole string as the var name.

To be more specific, the array looks like this (the last pair of [] is empty as i'd like to add new members to the array too):

$data = array(
                  [0] => array( //lets call this one item
                               'name' => Name,
                               'attribute' > Attribute,
                               'items' => array() // here can be x more items containing y more items

To store my current location in the array, I use another one (simple 1D array)

I have been coding for a few years in php data processing apps, but never had to solve this kind of thing :-)

Thanks for any help, if you need more information let me know pls.

8 Years
Discussion Span
Last Post by somedude3488


$myVar = "data[1]['items'][4]['items']";
$$myVar = 'xyz';

The two dollar signs I think is what you'll need, though I have never tried it with indexes like this.

hope that helps.


Although it did not work, thank you.
I think the main goal is that the array index must be told to the interpreter somehow else than the variable name.

When I tried it, then i just had a new var called "data[0][items]..." all this was in the name.

I will put here the answer if I found something.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.