Variable variables with dynamic array

Reply

Join Date: Sep 2009
Posts: 11
Reputation: gotschai is an unknown quantity at this point 
Solved Threads: 1
gotschai gotschai is offline Offline
Newbie Poster

Variable variables with dynamic array

 
0
  #1
33 Days Ago
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:
  1. $varName = "data";
  2. $varIndex = "[1]['items'][4]['items'][]";
  3.  
  4. ${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):

  1. $data = array(
  2. [0] => array( //lets call this one item
  3. 'name' => Name,
  4. 'attribute' > Attribute,
  5. '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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 62
Reputation: kylegetson is an unknown quantity at this point 
Solved Threads: 9
kylegetson's Avatar
kylegetson kylegetson is offline Offline
Junior Poster in Training
 
0
  #2
33 Days Ago
Try:
  1. $myVar = "data[1]['items'][4]['items']";
  2. $$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.
Don't pay data charges. txtFeeder.com is a free way to read the web on your mobile, and avoid data charges! **Now txtFeeder has a wireless note feature! Make notes on the go!
-Kyle Getson
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: gotschai is an unknown quantity at this point 
Solved Threads: 1
gotschai gotschai is offline Offline
Newbie Poster

Sorry but did not work

 
0
  #3
33 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso
 
0
  #4
32 Days Ago
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.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC