RE: [PHP] learning php - problem already
| From: | Ford, Mike [LSS] | Date: | Thu, 31 Jul 2003 14:18:24 +0000 |
| Subject: | RE: [PHP] learning php - problem already | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-157724@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: John W. Holmes [mailto:holmes072000@charter.net]
> Sent: 29 July 2003 23:05
>
> Curt Zirzow wrote:
> > Ok... I'm getting the red pen out now :)
> [snip]
> > the each() function returns a one element array that the current
> > (internal) array pointer is pointing to and will return false if at
> > the end of the array.
>
> It actually returns a four element array (as per the manual).
>
> > the list() function (not really a function) takes an array on the
> > right side of the = operator and assigns each variable its value in
> > order returned from the array.
>
> Right
>
> > so with the example array(0 => 'one', 1 => 'two'), the
> initial internal
> > pointer is looking at the first item so when the while statement
> > evaluates the the statement the each() function returns:
> > 0 => 'one'
>
> The four element array will be
> 1 => 'one'
> value => 'one'
> 0 => 0
> key => 0
OK, some more red pen coming along....
The four-element array would actually be:
0=>0
1=>'one'
'key'=>0
'value'=>'one'
in that order. So...
>
> > This array gets returned to the list statement
> > list($k, $v)
the list takes the first 2 elements (0=>0, 1=>'one') and assigns their values to $k
and $v respectively, giving $k==0, $v=='one' -- the remaining 2 elements are dropped
because there's nothing to assign them to. If you cared to put 4 variables in the list()
structure, thus:
list($k, $v, $key, $value) = each($a);
you would, in this case, now have $k==0, $v=='one', $key==0, $value=='one'.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211