473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dynamic Variable gets an Array Value

Hello,
I'm having some trouble creating a dynamic variable that will load an array value:

$this_content_json["content"][0]["popularity"]

When I use the above line 'hard coded', it works. But when I use the code below as a dynamic variable, it fails:

${ 'this_content_json["content"][0]["popularity"]' }

Any help would be muchos appreciados.

Tyler
Nov 28 '10 #1
11 2896
dlite922
1,584 Expert 1GB
what is so dynamic about the second example?

Are you trying to display it in a string?

Dan
Nov 29 '10 #2
Hi Dan,
Thanks for your reply. Once I get it working, I'm going to start changing the bits inside to access different array contents, which is my reason for using a dynamic array.

For example, I may do:
${ 'this_content_json[$a][$b][$c]' }

My issue at the moment is that the code I first posted isn't even accessing the basic contents in the original variable, so I think I'm making some rookie mistake.

This works, and echo's the value:
echo $this_content_json["content"][0]["popularity"];

But this doesn't:
echo ${ 'this_content_json["content"][0]["popularity"]' };

Any idea how I can properly recreate the variable in dynamic notation?

Tyler
Nov 30 '10 #3
code green
1,726 Expert 1GB
I am bewildered as to what you are trying to do.
Expand|Select|Wrap|Line Numbers
  1. //This works, and echo's the value:
  2. echo $this_content_json["content"][0]["popularity"];
This is legal syntax but I would like to see some justification for the need of a 3d array?
Expand|Select|Wrap|Line Numbers
  1. //But this doesn't:
  2. echo ${ 'this_content_json["content"][0]["popularity"]' };
This is incorrect syntax, but I can't correct it because I don;t see what you are trying to achieve.
I am not sure what you mean by dynamic variable, are you trying to use variable variables?
Dec 1 '10 #4
Hi Dan, thanks for staying with me here. Let me try again...

1) I have an multidimensional array of data called "$this_content_json".

2) I would like to access single pieces of data in that array using the standard way to do so - $this_content_json[$a][$b][$c] - where $a, $b, and $c are the names of the various keys in the array.

3) I am being handed those keys in a an unconventional format - a string that looks like: "[content][0][popularity]". I need to use that string variable to access the respective item in the array.

In summary, I have two things to work with - a string that looks like "[content][0][popularity]" and a var called "$this_content_json" that holds the content. The string changes depending on the instructions I am passed. I want to combine the two so I can access the relevant information in the array.

Kapiche?
Dec 1 '10 #5
code green
1,726 Expert 1GB
So this is a string and not an array
Expand|Select|Wrap|Line Numbers
  1. "[content][0][popularity]"
Where content is a key, popularity is a key and likewise 0 in the array $this_content_json.
And you want the value at reference $this_content_json['content'][0]['popularity'].
I see the problem. I suppose you could split the string using strtok or explode (or is it implode??)
so $a = content,$b = 0,$c = popularity.
Then reference the array with $this_content_json[$a][$b][$c].
But using the string directly? try one or all of these
Expand|Select|Wrap|Line Numbers
  1. echo $this_content_json.'["content"][0]["popularity"]';
  2.  
  3. $element = 'this_content_json["content"][0]["popularity"]';
  4. echo $$element;
  5.  
  6. $element = 'this_content_json';
  7. $element .= '["content"][0]["popularity"]';
  8. echo $$element; 
Or variations thereof. I am sure there is a correct format but it may need trial and error.
Dec 1 '10 #6
Thanks Code Green,
We`re on the same page now. I tested all three of the above proposed solutions, and none printed the variable.

I`m surprised none of these notations works. I have tried many additional variations myself with no luck. I thought I would at least be able to stumble on by luck if not skill...

I understand I can parse the string as suggested (strtok, explode, etc..), and will probably have to do so - it just seems like the long way to do it. I`m sure somehow there`s got to be a way to access it via the dynamic var format.

Anyhow thanks kindly for your ideas.

Tyler
Dec 2 '10 #7
code green
1,726 Expert 1GB
Actually the second two of my examples are wrong.
Don't know what I was thinking, but I have simply created variables of that name.
Dec 2 '10 #8
Yeah unfortunately the first one didn`t work either. I guess I`ll hack the parts apart.
Dec 2 '10 #9
code green
1,726 Expert 1GB
I am persevering with this because I'm sure I have done similar
OK this is a string
Expand|Select|Wrap|Line Numbers
  1. $ref = '["content"][0]["popularity"]';
That needs resolving as an array element.
This is the array $this_content_json;
This doesn't work
Expand|Select|Wrap|Line Numbers
  1. echo $this_content_json.'["content"][0]["popularity"]';
Because '["content"][0]["popularity"]' is still a string, it has not been resolved.
What about this
Expand|Select|Wrap|Line Numbers
  1. $element = $this_content_json.$ref; 
  2. echo $element;
Dec 2 '10 #10
Hey, thanks.

Unfortunately no dice. It prints out this instead of the proper data:

Array["content"][0]["popularity"]

As you can see the word Array is actually spelled out as a string (as php tends to do if you echo an array), followed by the string contents of the second part. I`m not sure why it`s not resolving to the array data instead.
Dec 2 '10 #11
Hey Code Green,
Thanks again for your efforts. While the above did not work, I found an explanation as to why:

http://bytes.com/topic/php/answers/1...ss-via-strings

It seems that is pretty much MUST be exploded before a string can be used to access a variable in the way I had hoped. So... not really possible after all.

Tyler
Feb 18 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jas | last post by:
Hi All I guess I should just have tried this to see if it works but I don't have access to a machine running IIS and can't test it and I am itching for an answer. I want to use dynamic...
6
by: alexandre damiron | last post by:
Did anyone tried dynamic variable naming in Js, like the ${$varname} feature of PHP ? It would easier a long DOM generation, for example to place such a code portion into a loop .... ...
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
8
by: Fernando Barsoba | last post by:
Hi, I decided to start a new topic about my memory allocation question. One of the answers was that variable length arrays are possible using C99. I found this topic that also mentions it. ...
1
by: JAG | last post by:
I am getting an error using the replace method in one of my functions. I am using the replace method in the mail document function in my frameset .hta to change forward slashes to back slashes in...
24
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
4
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on...
7
by: cnsabar | last post by:
Hi , I need to create a dynamic variable name in array using Perl... For example.. if i give the input no as 5.. The program has to create a variable name as @array1 @array2 @array3...
3
by: Keriana30 | last post by:
I need to base a variable array using a variable. For example, ReDim pstrDepSSN(pintRecordCount) as string The only way to do this is with the ReDim statement but it doesn't permit me to...
7
by: DavidSeck.com | last post by:
Hi guys, first post :) my question: is it possible to have dynamic variable names, I mean something like this: for($i=0;$i<x;$i++){ $y_$i = blabla; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.