Re: list() = each() Question

From: Date: Wed, 07 Feb 2001 16:08:54 +0000
Subject: Re: list() = each() Question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-38553@lists.php.net to get a copy of this message
Hehehe, I searched around on PHP.net, and found this tidbit of information. It sounds to me that list() is written to only accept the first two elements from the each(). Please correct me if I am misinformed :-) Cheers, Jason pyxl@dont_spam_on_me.jerrell.com 16-Feb-2000 03:41 Ok, for you folks who are learning this, here's something that should help your comprehension of each(), because I bashed my brains for a while on this one. The first example indicates that each() spits out a 4-cell 1 dimensional array. This is all fine and dandy until you get to the second example, where that seems to be thrown out the window, because though each() is still spitting out 4 array elements, the list() being used is set up to only accept 2 values, as it's being executed with only wo variables in it! For some folks, this might not be a problem, but I couldn't understand the mismatch - why was it done, and where did the array go that each() generated?? Well, upon executing that code, it turns out that the first two array elements of the 4 element array that each() creates are assigned to those two variables, and the last two array element values are just thrown away - they're totally ignored. It's how PHP is written. Now, why do that? Well, the example was definitely written more to show folks how to use each() to make life much easier when dealing with a particular operations array in PHP that a lot of people work with, but it also has the side effect (which hopefully my little explaination has made more palatable) of demonstrating how each() can act when being used with other functions that don't necessarily want all of each()'s input. ""Jason Mowat"" <jmowat@digitalpraxis.com> wrote in message news:95rrbg$p3i$1@toye.p.sourceforge.net... > Greets, > > I have a quick question: > > $Array = array( > "val1" => "Value 1", > "val2" => "Value 2", > "val3" => "Value 3" > ); > > while(list($a,$b,$c,$d) = each($Array)) { > print "$a = $c and $b = $d <br>"; > } > > Doesn't the "each" construct return a 4 element array, where element 1's > index is 0, element 2's index is 1, element 3's index is "key" and element > 4's index is "value"? Now, if that is the case, the above should work, > right? I should create a list containing $a, $b, $c and $d, where $a and $c > contain key information, and $b and $d contain value information? For some > reason, $c and $d are NULL... > > What am I missing here? > > Cheers, > Jason > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#38553) next »