473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array sorting differs depending on PHP version

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
2 2117
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
*** 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
4
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
2
by: J. J. Cale | last post by:
In IE6 both these functions *seem* to be working. I don't see much recursion in this group except the occasional setTimeout problems. Besides the obvious stack problems that can occur if one...
7
by: pauld | last post by:
Ive got a series of 2 dimensional associative arrays while(list($key, $val) = each ($results)) { {foreach($val as $i=>$v) print "$i = $v"; } } but i want to define the order that the...
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
13
by: pereges | last post by:
Hi, can some one please tell me why this program is not able to function properly. I have a array a and i am trying to create a pointer array b which points to elements less than 40 in a. ...
3
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.