|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #18299 arsort works different on PHP 4.1.2 and PHP 4.2.0
Submitted: 2002-07-12 05:19 UTC Modified: 2002-07-12 07:33 UTC
From: glimpse at glimpse dot fr dot fm Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.0 OS: Linux RedHat 6.2
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: glimpse at glimpse dot fr dot fm
New email:
PHP Version: OS:


 [2002-07-12 05:19 UTC] glimpse at glimpse dot fr dot fm
It seems like the arsort() function sort associative array strangely. The same script does not give the same result:

$fruits = array("d"=>"test","a"=>"test","b"=>"test","c"=>"test");
arsort ($fruits);
for (reset ($fruits); $key = key ($fruits); next ($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."\n";

Result with PHP 4.1.2 :

fruits[d] = test fruits[a] = test fruits[b] = test fruits[c] = test 
(with this order : d, a, b, c)

Result with PHP 4.2.0 :

fruits[c] = test fruits[b] = test fruits[a] = test fruits[d] = test
(with this order : c, b, a, d)

Why does the arsort() function change the order of the elements when they have the same value in PHP 4.2.0 while not in PHP 4.1.2?


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-12 05:47 UTC]
because we use a more efficient sort algorithm now

the result is different here, but still correct,
your array elements are perefectly sorted before
and after the asort() calls

whenever two elements are equal the sort algorithm
has the freedom to chose which to put first ...
 [2002-07-12 05:59 UTC] glimpse at glimpse dot fr dot fm
Ok, thanks.

But I have some code based on that. I want my array not to be sorted when the values are the same. Where is the advantage to change the order in that case? I think this sort of changes will affect a very large pool of web applications already installed with PHP 4.1.x and using this function... This kind of changes seems not to be important, but it is!

Best regards,
 [2002-07-12 06:13 UTC]
well, you relied on a behaviour never specified 
or garuanted :(

there is no 'advantage' in changing the order,
it just happens with the algorithm used and it is
not easy to avoid this

we could go over the result of the sort, look 
for elements with equal values and check in 
which order they did appear in the original
array, but that would waste most likely the
performans gains we got from switching to 
another algorithm

 [2002-07-12 07:33 UTC] glimpse at glimpse dot fr dot fm
Well, you're right.

I solved my problem: I make a loop to test if all the values are the same. In that case, I do not call the arsort() function and that works perfectly!
Thanks for your help and for replying so quickly!

Best regards,
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Dec 06 11:05:52 2022 UTC