Bug #18299 Updated: arsort works different on PHP 4.1.2 and PHP 4.2.0
| From: | glimpse at glimpse dot fr dot fm | Date: | Fri, 12 Jul 2002 11:33:57 +0000 |
| Subject: | Bug #18299 Updated: arsort works different on PHP 4.1.2 and PHP 4.2.0 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-13961@lists.php.net to get a copy of this message | ||
ID: 18299
Updated by: glimpse@glimpse.fr.fm
Reported By: glimpse@glimpse.fr.fm
Status: Bogus
Bug Type: Feature/Change Request
Operating System: Linux RedHat 6.2
PHP Version: 4.2.0
New Comment:
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,
Fred
Previous Comments:
------------------------------------------------------------------------
[2002-07-12 06:13:46] hholzgra@php.net
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 05:59:08] glimpse@glimpse.fr.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,
Fred
------------------------------------------------------------------------
[2002-07-12 05:47:07] hholzgra@php.net
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:19:10] glimpse@glimpse.fr.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?
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=18299&edit=1