note 68746 added to control-structures.foreach
| From: | epr01 at uow dot edu dot au | Date: | Wed, 09 Aug 2006 05:45:03 +0000 |
| Subject: | note 68746 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115716@lists.php.net to get a copy of this message | ||
PHP arrays are really handy, but because foreach loops through array cells in order of CREATION,
rather than in index order you cannot easily use them to implement priority queues. For example:
<?php
$priority_queue[1024] = 'sam';
$priority_queue[389] = 'sally';
$priority_queue[97] = 'tod';
$priority_queue[783] = 'mary';
foreach($priority_queue as $name) {
echo "hello $name !\n";
}
?>
Will NOT produce:
hello tod !
hello sally !
hello mary !
hello sam !
as you may wish.
----
Server IP: 203.202.10.60
Probable Submitter: 130.130.37.6 (proxied: 130.130.109.88)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/68746
Del: integrated -- https://master.php.net/note/delete/68746/integrated
Del: useless -- https://master.php.net/note/delete/68746/useless
Del: bad code -- https://master.php.net/note/delete/68746/bad+code
Del: spam -- https://master.php.net/note/delete/68746/spam
Del: non-english -- https://master.php.net/note/delete/68746/non-english
Del: in docs -- https://master.php.net/note/delete/68746/in+docs
Del: other reasons-- https://master.php.net/note/delete/68746
Reject -- https://master.php.net/note/reject/68746
Search -- https://master.php.net/manage/user-notes.php