note 102623 added to function.array-unshift

[email protected] Thu, 24 Feb 2011 14:51:31 -0800
Newsgroups php.notes
Message-ID <[email protected]>
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
----
Server IP: 69.147.83.197
Probable Submitter: 63.88.83.66
----
Manual Page -- http://www.php.net/manual/en/function.array-unshift.php
Edit        -- https://master.php.net/note/edit/102623
Del: integrated  -- https://master.php.net/note/delete/102623/integrated
Del: useless     -- https://master.php.net/note/delete/102623/useless
Del: bad code    -- https://master.php.net/note/delete/102623/bad+code
Del: spam        -- https://master.php.net/note/delete/102623/spam
Del: non-english -- https://master.php.net/note/delete/102623/non-english
Del: in docs     -- https://master.php.net/note/delete/102623/in+docs
Del: other reasons-- https://master.php.net/note/delete/102623
Reject      -- https://master.php.net/note/reject/102623
Search      -- https://master.php.net/manage/user-notes.php