This is a discussion on Help with Arrays. within the PHP Language forums, part of the PHP Programming Forums category; Hi! I'm kinda new to php and I am stumped by a dilemma. Okay I have a string called $...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi! I'm kinda new to php and I am stumped by a dilemma.
Okay I have a string called $word which contains "global" or any other word. Then I also have a set or arrays which is named after the strings in the $word variable like $global[] I also have a value called $tem5 depicting the current array index. A short version would be $global[1]="some string"; $word="global"; $temp5=1; How can I get the value of $global[1] then using the $word and $temp values? I tried $wordn = $word[$temp5]; <==obviously didn't work; parse_str("" . "worrn=(" . $word. "[" . $temp5 ."])"); <==somehow didn't work parse_str("" . "worrn=($" . $word. "[" . $temp5 ."])"); <==somehow didn't work parse_str("" . "worrn=(\$" . $word. "[" . $temp5 ."])"); <==somehow didn't work Then I tried to check if i can actually pass on the values $wordn = $global[1]; <==which worked. So how can I access an array if I have its name and index? Please help. |
|
|||
|
Janwillem Borleffs wrote:
> fogger schreef: > >> A short version would be >> >> $global[1]="some string"; >> $word="global"; >> $temp5=1; >> >> How can I get the value of $global[1] then using the $word and $temp >> values? >> > > $$word[$temp5]; > > Note the double $ before 'word'. > > > JW more correct way: $wordn = ${$word}[$temp5]; -- a.d. |
|
|||
|
Janwillem Borleffs wrote:
> Andrius Dijakas schreef: >> >> more correct way: >> $wordn = ${$word}[$temp5]; >> > > Doesn't make a difference, really. > > > JW but on my PHP v5.2.6 server I get "Notice: Undefined variable" with $$word[$temp5]; while no problem with ${$word}[$temp5]; -- a.d. |
|
|||
|
Andrius Dijakas schreef:
> but on my PHP v5.2.6 server I get "Notice: Undefined variable" with > $$word[$temp5]; > while no problem with > ${$word}[$temp5]; Indeed, I get the same problem with PHP4... The $$name notation of variable variables only seems to be valid for scalars. JW |
|
|||
|
On Thu, 08 May 2008 10:08:24 +0200, Janwillem Borleffs <jw@jwscripts.com>
wrote: > Andrius Dijakas schreef: >> but on my PHP v5.2.6 server I get "Notice: Undefined variable" with >> $$word[$temp5]; >> while no problem with ${$word}[$temp5]; > > Indeed, I get the same problem with PHP4... The $$name notation of > variable variables only seems to be valid for scalars. The problem is just that $word[$temp5] DOES exist, it means $word[1], and if $word = 'global', $word[1] = 'l' (lower case L for those that get confused with 1)), and $l obvisouly doesn't exist. The curly braces will tell PHP what portion should be the variable name, else it probably assumes ${$word[$temp5]}. A far less error-prone solution for the OP would obviously be: <?php $data = array(); $data['global'][1]="some string"; $word="global"; $temp5=1; echo $data[$word][$temp5]; ?> -- Rik Wasmus |
|
|||
|
On 8 May, 08:42, Andrius Dijakas <andri...@freemail.lt> wrote:
> Janwillem Borleffs wrote: > > Andrius Dijakas schreef: > > >> more correct way: > >> $wordn = ${$word}[$temp5]; > > > Doesn't make a difference, really. > > > JW > > but on my PHP v5.2.6 server I get "Notice: Undefined variable" with > $$word[$temp5]; > while no problem with > ${$word}[$temp5]; > > -- > a.d. I don't get an error (earlier PHP) but agree that ${$word}[$temp5] is more readable and avoid the ambigious interpretation ${$word[$temp5]} C. |