RE: [PHP] Empty elements in array

From: Date: Tue, 01 Aug 2000 07:11:00 +0000
Subject: RE: [PHP] Empty elements in array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-9369@lists.php.net to get a copy of this message
Because I wasn't interested in the key part. Quick explanation: list($a,$b,$c) = array(1,2,3); This would make the following assignments: $a = 1 $b = 2 $c = 3 You can grab just the 3rd element by doing: list(,,$c) = array(1,2,3); Now, if $foo = array(1,2,3) you are effectively doing: $foo[0] = 1 $foo[1] = 2 $foo[2] = 3 PHP maintains an internal array point. You can move it around using next(), prev(), end() and reset(). next($foo) moves the array pointer to the next position in the array and returns the value at that position. So: reset($foo); echo next($foo); would display: 2 each() is sort of like next(), except it return both the key and the value in a small 2-element array. reset($foo); $blah = each($foo); At this point $blah = array(0,1) since 0 was the index of the first element and 1 was the value. So, list(,$v) = each($foo) would just set $v to the value of the next item in the array. The reason for using each() instead of next() is that if the item happened to be 0 then next($foo) would return 0 and because 0 evaluates to false it isn't easy to tell the difference between that and the end of the array. With each() it will keep returning arrays until it hits the end of the array at which point it returns false. -Rasmus > Why the comma before $v in the example below? > > Noor > > -----Original Message----- > From: php-general-return-9332-noor=comrax.com@lists.php.net > [mailto:php-general-return-9332-noor=comrax.com@lists.php.net]On Behalf > Of Rasmus Lerdorf > Sent: eai uieue 01 aaaane 2000 03:34 > To: Tim > Cc: php-general@lists.php.net > Subject: Re: [PHP] Empty elements in array > > > > I have five elements in an array, and I have a while loop acting on the > > array. However, I only want the non-empty elements to be processed by the > > while loop - how do I tell it to ignore the empty elements? > > > > Thanks in advance for any help... :o) > > while(list(,$v)=each($array)) { > if($v) { > do something > } > } > > -Rasmus > > >

« previous php.general (#9369) next »