From: "Ford, Mike [LSS]" <[EMAIL PROTECTED]>
> From: John W. Holmes [mailto:[EMAIL PROTECTED]
> > 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...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to