By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

Array sorting differs depending on PHP version

P: n/a
I have this test code:
<html><pre><?

echo "Versión: ".phpversion()."\n\n";

$define_list = array(
'PRODUCT_LIST_MODEL' => 0,
'PRODUCT_LIST_NAME' => 0,
'PRODUCT_LIST_MANUFACTURER' => 1,
'PRODUCT_LIST_PRICE' => 0,
'PRODUCT_LIST_QUANTITY' => 1,
'PRODUCT_LIST_WEIGHT' => 1,
'PRODUCT_LIST_IMAGE' => 1,
'PRODUCT_LIST_BUY_NOW' => 1
);
arsort($define_list);
echo "*** arsort(): ";
print_r($define_list);
echo "\n";

$define_list = array(
'PRODUCT_LIST_MODEL' => 0,
'PRODUCT_LIST_NAME' => 0,
'PRODUCT_LIST_MANUFACTURER' => 1,
'PRODUCT_LIST_PRICE' => 0,
'PRODUCT_LIST_QUANTITY' => 1,
'PRODUCT_LIST_WEIGHT' => 1,
'PRODUCT_LIST_IMAGE' => 1,
'PRODUCT_LIST_BUY_NOW' => 1
);
asort($define_list);
echo "*** asort(): ";
print_r($define_list);
echo "\n";
?>
In two different servers I obtain different results:
Versión: 4.1.2

*** arsort(): Array
(
[PRODUCT_LIST_MANUFACTURER] => 1
[PRODUCT_LIST_QUANTITY] => 1
[PRODUCT_LIST_WEIGHT] => 1
[PRODUCT_LIST_IMAGE] => 1
[PRODUCT_LIST_BUY_NOW] => 1
[PRODUCT_LIST_MODEL] => 0
[PRODUCT_LIST_NAME] => 0
[PRODUCT_LIST_PRICE] => 0
)

*** asort(): Array
(
[PRODUCT_LIST_MODEL] => 0
[PRODUCT_LIST_NAME] => 0
[PRODUCT_LIST_PRICE] => 0
[PRODUCT_LIST_MANUFACTURER] => 1
[PRODUCT_LIST_QUANTITY] => 1
[PRODUCT_LIST_WEIGHT] => 1
[PRODUCT_LIST_IMAGE] => 1
[PRODUCT_LIST_BUY_NOW] => 1
)

Versión: 4.2.2

*** arsort(): Array
(
[PRODUCT_LIST_QUANTITY] => 1
[PRODUCT_LIST_IMAGE] => 1
[PRODUCT_LIST_MANUFACTURER] => 1
[PRODUCT_LIST_BUY_NOW] => 1
[PRODUCT_LIST_WEIGHT] => 1
[PRODUCT_LIST_PRICE] => 0
[PRODUCT_LIST_NAME] => 0
[PRODUCT_LIST_MODEL] => 0
)

*** asort(): Array
(
[PRODUCT_LIST_MODEL] => 0
[PRODUCT_LIST_PRICE] => 0
[PRODUCT_LIST_NAME] => 0
[PRODUCT_LIST_BUY_NOW] => 1
[PRODUCT_LIST_IMAGE] => 1
[PRODUCT_LIST_QUANTITY] => 1
[PRODUCT_LIST_MANUFACTURER] => 1
[PRODUCT_LIST_WEIGHT] => 1
)

According to some user contributed notes (manual itself says nothing about
this) I can't expect to maintain the relative order of keys. Yet I can't
find the logic of the algorithm. Why do I get different key sortings in
versions 4.1.2 and 4.2.2?
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The key order is not maintained between versions because the code is
optimised between versions, and this improvement (though it doesn't
sound like you'd choose that word) has returned the keys in a different
arbitrary order. But the fact is, the key order seems to always have
been arbitrary, so if key order matters, asort (and arsort) are not the
functions for you.

ksort() sorts by key while maintaining key=>value relations, so that may
be of interest to you. However, it sounds more like you just want to
have a consistent order of keys while still being left with a sorted set
of values. For this I believe you would have to use usort() and define
your own sorting function. That way you could decide how to sort things
once and for all. E.g. use asort to group the set into a value-sorted
set, then go through the value of each one, and each time the value
changes, sort the preceeding values using ksort() to sort same-vaule
entries by key. That may be costly, though, and I may not be seeing an
easier route.
--
Bob
London, UK
echo Mail fefsensmrrjyaheeoceoq\! | tr "jefroq\!" "@obe.uk"
Jul 17 '05 #2

P: n/a
*** Robert Downes wrote/escribió (Thu, 11 Dec 2003 21:56:04 +0000):
The key order is not maintained between versions because the code is
optimised between versions, and this improvement (though it doesn't
sound like you'd choose that word) has returned the keys in a different
arbitrary order. But the fact is, the key order seems to always have
been arbitrary, so if key order matters, asort (and arsort) are not the
functions for you.


Thank you for your answer. I guess that's the logic of the algorithm: it
isn't the same algorithm :)

There doesn't seem to be a pre-written function that sorts values keeping
the relative order of keys but user notes provide with some ideas. Anyway,
I've checked the code again (it's a code I didn't write) and realized that
I don't probably need any kind of sorting once I decide an order for the
fields.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.