Re: Arrays
| From: | Cody Caughlan | Date: | Mon, 31 Jul 2000 21:05:08 +0000 |
| Subject: | Re: Arrays | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-1652@lists.php.net to get a copy of this message | ||
you can mix indexed arrays and associative arrays together, so I could have
any array like this:
fruit = array (
0 => "apple",
1 => "orange",
2 => "pear",
"color" = > "orange",
'texture" => "bumpy");
so, we have 5 elements in this array (0-4), if I wanted to get 'bumpy' and
I knew it was at index 4 I could access it 2 ways:
1) $texture = $fruit[4];
2) $texture = $fruit[texture];
so, using
while(list($key, $val) = each($fruit))
{
the each statement looks at each element of the array, each element
has 2 parts, its index and its value,
the first element has index 0 and value 'apple', the second element
has index 1 and value 'orange',
so, using the above while loop with list and each, you can access it
element, $key becomes 0,1,2,'color','texture' and $val becomes
'apple',
'orange', 'pear', 'orange', 'bumpy' during its entire
life-cycle (one at a
time though)
}
does this make sense,
/Cody
BTW, if you have any more general PHP questions, please direct them to the
PHP mailing list rather than the database PHP mailing list. Thanks
Tim wrote:
> > > What's the easiest way to loop through an array? I am using PHP3 - I
> know
> > > that PHP4 has a "foreach" structure, but sadly this is not there in
> version
> > > 3. I know it's probably something to do with a "while" loop - but how
> > > exactly is it done? I have an array of 5 variables, and some of the
> > > variables may be empty.
> >
> > while(list($key,$value)=each($array)) {
> > echo "$key: $value<br>\n";
> > }
> >
> > -Rasmus
>
> Thanks... I'm a bit of a newbie, so I'm not sure what the $key variable
> does... is it the index for the array?
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
> For additional commands, e-mail: php-db-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
Cody Caughlan -codyc@cisco.com- Cisco Systems
Web/Database Engineer phone: (408) 525-6029
Enterprise Line of Business pager: (408) 951-9949