This is a discussion on RE: [PHP] learning php - problem already within the PHP General forums, part of the PHP Programming Forums category; > -----Original Message----- > From: John W. Holmes [mailto:holmes072000@charter.net] > Sent: 29 July 2003 23:05 > &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
> -----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 |
|
|||
|
From: "Ford, Mike [LSS]" <M.Ford@lmu.ac.uk>
> From: John W. Holmes [mailto:holmes072000@charter.net] > > The four element array will be > > 1 => 'one' > > value => 'one' > > 0 => 0 > > key => 0 > > OK, some more red pen coming along.... Since we're whipping them out (red pens that is) > The four-element array would actually be: > > 0=>0 > 1=>'one' > 'key'=>0 > 'value'=>'one' Nope. Try this example from the manual and you'll see that you get the order I gave $foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each ($foo); print_r($bar); The order is pretty irrelevant, though. > list($k, $v, $key, $value) = each($a); > > you would, in this case, now have $k==0, $v=='one', $key==0, $value=='one'. Nope.. try that and you'll get two notices about undefined offsets at 3 and 2. $key and $value will not have any value at all. Not that this really matters, but since this is fun to argue about... $a = array('a' => 'abc'); Since we know that each($a) will return what I gave above, you basically have this: list($k,$v,$key,$value) = array(1=>'abc', 'value'=>'abc', 0=>'a', 'key'=>'a'); So, how this works is that list starts with $value. $value is at position number four, so since arrays start at zero, it's going to look in the array that was passed for an element at [3]. Since there is no [3] in the passed array, you get a NOTICE about undefined offset. Next it moves on to $key and looks for element [2]. Again you get a warning since there is no element [2] in the passed array. Next is $v and list is looking for [1]. Since [1] does exist and has a value of 'abc', now $v = 'abc' Last is $k and [0] and you get $k = 'a'. That's how it works. :) That's why this code: list($a, $b, $c, $d) = array(4=>'four', 3=>'three', 2=>'two', 1=>'one', 0=>'zero'); echo "$a, $b, $c, $d"; gives: zero, one, two, three as the result, even though you passed a five element array in reverse order. ---John Holmes... |