#21788 [Sus->WFx]: array_multisort() changes array keys unexpectedly given numeric strings as keys

From: Date: Wed, 16 Jul 2003 00:52:27 +0000
Subject: #21788 [Sus->WFx]: 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-44073@lists.php.net to get a copy of this message
 ID:               21788
 Updated by:       sniper@php.net
 Reported By:      jon at inet-specialists dot com
-Status:           Suspended
+Status:           Wont fix
 Bug Type:         Scripting Engine problem
 Operating System: NT 4.0 SP6a
 PHP Version:      4.3.0
 New Comment:

correct status.



Previous Comments:
------------------------------------------------------------------------

[2003-02-04 14:19:29] 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
)


------------------------------------------------------------------------

[2003-01-20 21:58:09] 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>

------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=21788&edit=1



Thread (5 messages)

« previous php.bugs (#44073) next »