note 74129 added to function.array-unshift
| From: | amschroeder at gmail dot com | Date: | Mon, 26 Mar 2007 17:13:05 +0000 |
| Subject: | note 74129 added to function.array-unshift | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124824@lists.php.net to get a copy of this message | ||
This becomes a nice little problem if you index your arrays out of order (while manually sorting).
For example:
$recordMonths[3] = '8/%/2006';
$recordMonths[4] = '7/%/2004';
$recordMonths[0] = '3/%/2007';
$recordMonths[1] = '2/%/2007';
$recordMonths[5] = '12/%/2000';
$recordMonths[6] = '11/%/2000';
$recordMonths[7] = '10/%/2000';
$recordMonths[2] = '1/%/2007';
for($i = 0; $i < count($recordMonths); $i++)
{
$singleMonth = $recordMonths[$i];
echo "singleMonth: $singleMonth <br />";
}
array_unshift($recordMonths,'%');
for($i = 0; $i < count($recordMonths); $i++)
{
$singleMonth = $recordMonths[$i];
echo "singleMonth: $singleMonth <br />";
}
Produces:
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 1/%/2007
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: %
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: 1/%/2007
It reindexes them based on the order they were created. It seems like if an array has all numeric
indexes, then it should reindex them based on the order of their index. Just my opinion...
----
Server IP: 64.71.164.2
Probable Submitter: 169.237.157.182
----
Manual Page -- http://www.php.net/manual/en/function.array-unshift.php
Edit -- https://master.php.net/note/edit/74129
Del: integrated -- https://master.php.net/note/delete/74129/integrated
Del: useless -- https://master.php.net/note/delete/74129/useless
Del: bad code -- https://master.php.net/note/delete/74129/bad+code
Del: spam -- https://master.php.net/note/delete/74129/spam
Del: non-english -- https://master.php.net/note/delete/74129/non-english
Del: in docs -- https://master.php.net/note/delete/74129/in+docs
Del: other reasons-- https://master.php.net/note/delete/74129
Reject -- https://master.php.net/note/reject/74129
Search -- https://master.php.net/manage/user-notes.php