php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17869 Array sort order broken with more than 256 entries in an associative array?
Submitted: 2002-06-20 04:50 UTC Modified: 2002-06-20 05:29 UTC
From: adamek at webtrio dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.2.1 OS: Linux 2.4.16
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: adamek at webtrio dot de
New email:
PHP Version: OS:

 

 [2002-06-20 04:50 UTC] adamek at webtrio dot de
To reproduce the problem do the following:

1) Read a mysql database table with user names and a double index (UID, server - both numeric) having about 280 entries:

SELECT UID,Server,Username FROM Users ORDER BY UID,Server

2) Preparing the array: $userlist = array();

3) Filling the array:

$result = mysql_query("SELECT UID,Server,Username FROM Users ORDER BY UID,Server");

if ($result)
{
   while ($q = get_object($result))
   {
       $userlist[$q->UID."-".$q->Server] = $q->Username." (".$q->Server.")";
       
       // Debug output just to see that filling happens in the right alphabetical order of user names
       echo $userlist[$q->UID."-".$q->Server]."\n";
   }
}

4) Reading the array:

foreach ($userlist as $key => $value)
{
   echo $key." = ".$value."\n";
}

you suddenly notice that the array entry no. 256 and 257 appear at the wrong place (at position count($userlist)-256) - the internal sort order of the array has changed!

I also tried this with array index names in the form:

[$q->UID.":".$q->Server]

and

[$q->UID.".".$q->Server]

Both with the same wrong result ... arrays read out don't have the same order as they were filled any more if more than 256 entries are present.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-20 04:55 UTC] adamek at webtrio dot de
Mistype in my bug report ;-)

The line

while ($q = get_object($result))

must be

while ($q = mysql_fetch_object($result))

of course ..... forgot to replace my mysql query wrapper code with real mysql_ function ....

Of course bug is still present - has nothing to do with mysql ...
 [2002-06-20 05:07 UTC] adamek at webtrio dot de
The bug still stays when replacing some lines:

Replace the line

$userlist[$q->UID."-".$q->Server] = $q->Username."
(".$q->Server.")";

by the lines:
$arraykey = $q->UID.".".$q->Server;
$arrayvalue = $q->Username." (".$q->Server.")";

$userlist[$arraykey] = $arrayvalue;

(key and value prepared)
 [2002-06-20 05:14 UTC] jan@php.net
can please submit a small script along with some test data (please just link to a page you set up, don't include the test data in the bugreport.

Thank you for your interest in PHP.
 [2002-06-20 05:18 UTC] adamek at webtrio dot de
Sorry Jan, it was a bug in the database structure .... key with the UID value existed twice ..... bug closed
 [2002-06-20 05:29 UTC] jan@php.net
if it was not a bug the report is "bogus". Thank you.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC