note 90683 deleted from control-structures.foreach by googleguy
| From: | googleguy@php.net | Date: | Wed, 14 Nov 2012 22:28:12 +0000 |
| Subject: | note 90683 deleted from control-structures.foreach by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191963@lists.php.net to get a copy of this message | ||
Note Submitter: burleigh at chorus dot net
----
Using numeric indexes doesn't automatically sort your array.
<?php
$titles[3] = 'three';
$titles[2] = 'two';
$titles[1] = 'one';
print_r( $titles);
foreach ($titles as $t ) {
print "title=$t ";
}
?>
will display:
Array ( [3] => three [2] => two [1] => one )
title=three title=two title=one
If you want the items coming out in the correct order you need to use sort
<?php
sort($titles);
print_r($titles);
?>
Array ( [0] => one [1] => three [2] => two )