php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21788 array_multisort() changes array keys unexpectedly given numeric strings as keys
Submitted: 2003-01-20 21:58 UTC Modified: 2003-07-15 19:52 UTC
Votes:9
Avg. Score:4.9 ± 0.3
Reproduced:9 of 9 (100.0%)
Same Version:4 (44.4%)
Same OS:1 (11.1%)
From: jon at inet-specialists dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4.3.0 OS: NT 4.0 SP6a
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
31 + 44 = ?
Subscribe to this entry?

 
 [2003-01-20 21:58 UTC] jon at inet-specialists dot 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>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-04 14:19 UTC] pollita@php.net
This is due to Zend casting the array key as a numeric in the array declarations.  When array_multisort calls zend_qsort any numeric keys are reassigned beginning with 0.

I'm reclassifying this as a Suspended Scripting Engine Bug for now (pending discussion), though it's technically expected behavior, and may eventually be reflagged as "Won't Fix".

For the moment I'm going to say prefix all your array keys with an extra 0 (or any non-numeric) to force their casting as strings.

For the sake of readability by others looking at this bug, the behavior can be recreated with the following - simpler - script.

<?php
  $a = array('000001'=>"Joe",
             '100002'=>"Joe",
             '000003'=>"Frank");
  $b = array('000001'=>"Smith",
             '100002'=>"Jones",
             '000003'=>"Frederick");
  array_multisort($a,$b);
  print_r($a);
  print_r($b);
?>

The expected output would be:
Array
(
    [000003] => Frank
    [100002] => Joe
    [000001] => Joe
)
Array
(
    [000003] => Frederick
    [100002] => Jones
    [000001] => Smith
)


The actual output (because of key type casting) is:
Array
(
    [000003] => Frank
    [0] => Joe
    [000001] => Joe
)
Array
(
    [000003] => Frederick
    [0] => Jones
    [000001] => Smith
)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 15:01:28 2024 UTC