note 102624 deleted from function.array-unshift by danbrown

From: Date: Thu, 24 Feb 2011 22:52:44 +0000
Subject: note 102624 deleted from function.array-unshift by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177311@lists.php.net to get a copy of this message
Note Submitter: winklerd at gmail dot com ---- To answer amschroeder at gmail dot com from below, if you run a ksort after inserting numeric indexes out of order, it will work properly. <?php $arr1[0] = "first"; $arr1[2] = "last"; $arr1[1] = "middle"; for($i = 0; $i < count($arr1); $i++) { print "$i: {$arr1[$i]}\n"; } print "\n"; array_unshift($arr1,"new"); for($i = 0; $i < count($arr1); $i++) { print "$i: {$arr1[$i]}\n"; } print "\n\n"; $arr2[0] = "first"; $arr2[2] = "last"; $arr2[1] = "middle"; ksort($arr2); for($i = 0; $i < count($arr2); $i++) { print "$i: {$arr2[$i]}\n"; } print "\n"; array_unshift($arr2,"new"); for($i = 0; $i < count($arr2); $i++) { print "$i: {$arr2[$i]}\n"; } ?> produces: 0: first 1: middle 2: last 0: new 1: first 2: last 3: middle 0: first 1: middle 2: last 0: new 1: first 2: middle 3: last

« previous php.notes (#177311) next »