#21788 [Opn]: array_multisort() changes array keys unexpectedly given numeric strings as keys
| From: | jon at inet-specialists dot com | Date: | Tue, 21 Jan 2003 04:12:42 +0000 |
| Subject: | #21788 [Opn]: array_multisort() changes array keys unexpectedly given numeric strings as keys | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-30776@lists.php.net to get a copy of this message | ||
ID: 21788
User updated by: jon@inet-specialists.com
Reported By: jon@inet-specialists.com
Status: Open
Bug Type: Arrays related
Operating System: NT 4.0 SP6a
PHP Version: 4.3.0
New Comment:
Sorting these 2 arrays
Array
(
[000001] => Personal, Master
[000002] => Information, Gorean
[000003] => Information, Gorean
[000004] => Books, Information
[000005] => Books, Information
[000006] => Information, Gorean
[000007] => Information, Gorean
[000008] => Books, Information
[000009] => Books, Information
[000010] => Group, Offline
[000011] => Internet, Link Collection
[120011] => Internet, Link Exchange
[000013] => Books, Discussion
[000014] => Books, Discussion
[150014] => Books, Discussion
[000015] => Books, Discussion
[000016] => Books, Discussion
[000017] => Books, Discussion
[000018] => Books, Discussion
)
Array
(
[000001] => APPROVED
[000002] => APPROVED
[000003] => APPROVED
[000004] => APPROVED
[000005] => APPROVED
[000006] => APPROVED
[000007] => APPROVED
[000008] => APPROVED
[000009] => APPROVED
[000010] => APPROVED
[000011] => APPROVED
[120011] => APPROVED
[000013] => APPROVED
[000014] => BROKEN
[150014] => BROKEN
[000015] => APPROVED
[000016] => BROKEN
[000017] => BROKEN
[000018] => BROKEN
)
Results in changed indices in the arrays
Array
(
[000013] => Books, Discussion
[000015] => Books, Discussion
[000014] => Books, Discussion
[0] => Books, Discussion
[000016] => Books, Discussion
[000017] => Books, Discussion
[000018] => Books, Discussion
[000009] => Books, Information
[000008] => Books, Information
[000004] => Books, Information
[000005] => Books, Information
[000010] => Group, Offline
[000003] => Information, Gorean
[000002] => Information, Gorean
[000006] => Information, Gorean
[000007] => Information, Gorean
[000011] => Internet, Link Collection
[1] => Internet, Link Exchange
[000001] => Personal, Master
)
Array
(
[000013] => APPROVED
[000015] => APPROVED
[000014] => BROKEN
[0] => BROKEN
[000016] => BROKEN
[000017] => BROKEN
[000018] => BROKEN
[000009] => APPROVED
[000008] => APPROVED
[000004] => APPROVED
[000005] => APPROVED
[000010] => APPROVED
[000003] => APPROVED
[000002] => APPROVED
[000006] => APPROVED
[000007] => APPROVED
[000011] => APPROVED
[1] => APPROVED
[000001] => APPROVED
)
Previous Comments:
------------------------------------------------------------------------
[2003-01-20 22:05:01] jon@inet-specialists.com
<script language="php">
/*
The following code produces an unexpected result from the
array_multisort() function.
The indices of the arrays are are modified for the two entries in which
the numeric value reaches the first position of the string index.
Granted, all of the indices are numerical, and therefore may be
assigned to an integer when converted; however, the expected
replacement would be the integer value of the string representation.
(i.e. "150014" would become 150014)
In actuality, the indices "150014" and "120011" are converted to 1 and
0 respectively. Therefore, the associative array is no longer
associative and the indices do not relate to the original arrays, even
though the sort order is correct and the two arrays still relate to
each other.
*/
$Category = array(
"000001" => "Personal, Master", "000002" => "Information,
Gorean",
"000003" => "Information, Gorean", "000004" => "Books,
Information",
"000005" => "Books, Information", "000006" =>
"Information, Gorean",
"000007" => "Information, Gorean", "000008" => "Books,
Information",
"000009" => "Books, Information", "000010" => "Group,
Offline",
"000011" => "Internet, Link Collection", "120011" =>
"Internet, Link
Exchange",
"000013" => "Books, Discussion", "000014" => "Books,
Discussion",
"150014" => "Books, Discussion", "000015" => "Books,
Discussion",
"000016" => "Books, Discussion", "000017" => "Books,
Discussion",
"000018" => "Books, Discussion" );
$LinksStatus = array (
"000001" => "APPROVED", "000002" => "APPROVED",
"000003" =>
"APPROVED",
"000004" => "APPROVED", "000005" => "APPROVED",
"000006" =>
"APPROVED",
"000007" => "APPROVED", "000008" => "APPROVED",
"000009" =>
"APPROVED",
"000010" => "APPROVED", "000011" => "APPROVED",
"120011" =>
"APPROVED",
"000013" => "APPROVED", "000014" => "BROKEN",
"150014" =>
"BROKEN",
"000015" => "APPROVED", "000016" => "BROKEN",
"000017" =>
"BROKEN",
"000018" => "BROKEN" );
echo("<h3>Sorting these 2 arrays</h3>\n<pre>");
print_r($Category);
print_r($LinksStatus);
echo("</pre>");
// I DO NOT KNOW WHY THE FOLLOWING LINE REASSIGNS INDEXES OF THE ARRAY
array_multisort ($Category, SORT_STRING, SORT_ASC,
$LinksStatus, SORT_STRING, SORT_DESC);
echo("<h3>Results in changed indices in the arrays</h3>\n<pre>");
print_r($Category);
print_r($LinksStatus);
echo("</pre>");
</script>
------------------------------------------------------------------------
[2003-01-20 21:58:09] jon@inet-specialists.com
<script language="php">
/*
The following code produces an unexpected result from the
array_multisort()
function.
The indices of the arrays are are modified for the two entries
in which the numeric value reaches the first position of the string
index.
Granted, all of the indices are numerical, and therefore may be
assigned to
and integer when converted; however, the expected replacement would be
the
integer value of the string representation. (i.e. "150014" would
become 150014)
In actuality, the indices "150014" and "120011" are converted to 1 and
0
respectively. Therefore, the associative array is no longer
associative and
the indices do not relate to the original arrays, even though the sort
order
is correct and the two arrays still relate to each other.
*/
$Category = array (
"000001" => "Personal, Master", "000002" =>
"Information, Gorean",
"000003" => "Information, Gorean", "000004" => "Books,
Information",
"000005" => "Books, Information", "000006" =>
"Information,
Gorean",
"000007" => "Information, Gorean", "000008" => "Books,
Information",
"000009" => "Books, Information", "000010" => "Group,
Offline",
"000011" => "Internet, Link Collection", "120011" =>
"Internet, Link
Exchange",
"000013" => "Books, Discussion", "000014" => "Books,
Discussion",
"150014" => "Books, Discussion", "000015" => "Books,
Discussion",
"000016" => "Books, Discussion", "000017" => "Books,
Discussion",
"000018" => "Books, Discussion" );
$LinksStatus = array (
"000001" => "APPROVED", "000002" => "APPROVED",
"000003" =>
"APPROVED",
"000004" => "APPROVED", "000005" => "APPROVED",
"000006" =>
"APPROVED",
"000007" => "APPROVED", "000008" => "APPROVED",
"000009" =>
"APPROVED",
"000010" => "APPROVED", "000011" => "APPROVED",
"120011" =>
"APPROVED",
"000013" => "APPROVED", "000014" => "BROKEN",
"150014" =>
"BROKEN",
"000015" => "APPROVED", "000016" => "BROKEN",
"000017" =>
"BROKEN",
"000018" => "BROKEN" );
echo("<h3>Sorting these 2 arrays</h3>\n<pre>");
print_r($Category);
print_r($LinksStatus);
echo("</pre>");
// I DO NOT KNOW WHY THE FOLLOWING LINE REASSIGNS INDEXES OF THE ARRAY
!!!
array_multisort ( $Category, SORT_STRING, SORT_ASC,
$LinksStatus, SORT_STRING, SORT_DESC);
echo("<h3>Results in changed indices in the arrays</h3>\n<pre>");
print_r($Category);
print_r($LinksStatus);
echo("</pre>");
</script>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=21788&edit=1