Array sorting differs depending on PHP version 
July 17th, 2005, 02:35 AM
| | | |
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
-- | 
July 17th, 2005, 02:36 AM
| | | | re: Array sorting differs depending on PHP version
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" | 
July 17th, 2005, 02:36 AM
| | | | re: Array sorting differs depending on PHP version
*** Robert Downes wrote/escribió (Thu, 11 Dec 2003 21:56:04 +0000):[color=blue]
> 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.[/color]
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
-- |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,720 network members.
|