note 113513 added to function.key
| From: | vinob44 at gmail dot com | Date: | Wed, 23 Oct 2013 11:43:33 +0000 |
| Subject: | note 113513 added to function.key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197303@lists.php.net to get a copy of this message | ||
Suppose if the array values are in numbers and numbers contains
0 then the loop will be
terminated. To overcome this you can user like this
<?php
$array = array(
'0' => '5',
'1' => '2',
'2' => '0',
'3' => '3',
'4' => '1');
// wrong approach
while ($fruit_name = current($array)) {
echo key($array).'<br />';
next($array);
}
// the way will be break loop when arra('2'=>0) because its value is '0',
while(0) will terminate the loop
// correct approach
while ( ($fruit_name = current($array)) !== FALSE ) {
echo key($array).'<br />';
next($array);
}
//this will work properly
?>
----
Server IP: 69.147.83.199
Probable Submitter: 202.88.246.134
----
Manual Page -- http://php.net/manual/en/function.key.php
Edit -- https://master.php.net/note/edit/113513
Del: integrated -- https://master.php.net/note/delete/113513/integrated
Del: useless -- https://master.php.net/note/delete/113513/useless
Del: bad code -- https://master.php.net/note/delete/113513/bad+code
Del: spam -- https://master.php.net/note/delete/113513/spam
Del: non-english -- https://master.php.net/note/delete/113513/non-english
Del: in docs -- https://master.php.net/note/delete/113513/in+docs
Del: other reasons-- https://master.php.net/note/delete/113513
Reject -- https://master.php.net/note/reject/113513
Search -- https://master.php.net/manage/user-notes.php