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.

Try:

$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.

You either need to use eval (which is slow and has security risks if not used properly) or you loop through the array until you get to the index you want.

Here is an example. You should find what you are looking for there.
http://www.daniweb.com/forums/thread191093.html.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.