Help with Arrays.

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-08-2008
fogger
 
Posts: n/a
Default Help with Arrays.

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.
Reply With Quote
  #2 (permalink)  
Old 05-08-2008
Janwillem Borleffs
 
Posts: n/a
Default Re: Help with Arrays.

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
Reply With Quote
  #3 (permalink)  
Old 05-08-2008
Andrius Dijakas
 
Posts: n/a
Default Re: Help with Arrays.

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.
Reply With Quote
  #4 (permalink)  
Old 05-08-2008
Janwillem Borleffs
 
Posts: n/a
Default Re: Help with Arrays.

Andrius Dijakas schreef:
>
> more correct way:
> $wordn = ${$word}[$temp5];
>


Doesn't make a difference, really.


JW
Reply With Quote
  #5 (permalink)  
Old 05-08-2008
Andrius Dijakas
 
Posts: n/a
Default Re: Help with Arrays.

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.
Reply With Quote
  #6 (permalink)  
Old 05-08-2008
Janwillem Borleffs
 
Posts: n/a
Default Re: Help with Arrays.

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
Reply With Quote
  #7 (permalink)  
Old 05-08-2008
Rik Wasmus
 
Posts: n/a
Default Re: Help with Arrays.

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
Reply With Quote
  #8 (permalink)  
Old 05-08-2008
fogger
 
Posts: n/a
Default Re: Help with Arrays.

Thanks for the quick replies!

Very much appreciated. :)
Reply With Quote
  #9 (permalink)  
Old 05-08-2008
C. (http://symcbean.blogspot.com/)
 
Posts: n/a
Default Re: Help with Arrays.

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:57 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0