note 90683 added to control-structures.foreach

From: Date: Mon, 04 May 2009 17:21:41 +0000
Subject: note 90683 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-154200@lists.php.net to get a copy of this message
Using numeric indexes doesn't automatically sort your array. <? $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 ) ---- Server IP: 69.147.83.197 Probable Submitter: 64.73.12.253 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/90683 Del: integrated -- https://master.php.net/note/delete/90683/integrated Del: useless -- https://master.php.net/note/delete/90683/useless Del: bad code -- https://master.php.net/note/delete/90683/bad+code Del: spam -- https://master.php.net/note/delete/90683/spam Del: non-english -- https://master.php.net/note/delete/90683/non-english Del: in docs -- https://master.php.net/note/delete/90683/in+docs Del: other reasons-- https://master.php.net/note/delete/90683 Reject -- https://master.php.net/note/reject/90683 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154200) next »