note 68746 deleted from control-structures.foreach by betz
| From: | betz@php.net | Date: | Wed, 09 Aug 2006 07:55:54 +0000 |
| Subject: | note 68746 deleted from control-structures.foreach by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115718@lists.php.net to get a copy of this message | ||
Note Submitter: epr01 at uow dot edu dot au
----
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.