From: cataphract Operating system: Irrelevant PHP version: trunk-SVN-2011-06-29 (SVN) Package: Scripting Engine problem Bug Type: Bug Bug description:comparison function is not anticommutative for arrays claimed to be comparable
Description: ------------ This discussion is limited to the subsets of the array domain with elements that the manual claims are comparable between each other: «Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value» http://docs.php.net/manual/en/language.operators.comparison.php Whether the relations should be total (or partial) orders in the entire array domain is beyond the scope of this bug report. The comparison function used to compare arrays with the operators, <, <=, >, >= is not anticommutative, i.e., swapping the arguments doesn't change the sign. It should be the case that cmp(a, b) = - cmp(b, a) The reason for this is that the relations <, <=, >, >= should be antisymmetric, i.e., R(a, b) and R(b, a) cannot both be true unless maybe a == b (in which case the relation is reflexive, as is the case of <= and >=). The relationship between the comparison function and the relations easily follows by how the relations are defined: a < b === cmp(a,b) == -1 a <= b === cmp(a,b) <= 0 a > b === cmp(b,a) == -1 a >= b === cmp(b,a) <= 0 It is important that the relations are antisymmetric because it's a requirement for relation to be a total (or even partial!) order. Without that property, it's impossible to, for instance, reliably sort or do a binary search in an array of arrays. The problem is the comparison function is sensitive to the order of the arguments. It starts by comparing the value of the first element of the first operand with the corresponding (i.e., with the same key) element in the second operand. One can see that, for any two arrays with the same length and keys, if reset($a) < $b[key($a)] && reset($b) < $x[key($a)], then $a < $b and $b < $a. References: http://en.wikipedia.org/wiki/Anticommutativity http://en.wikipedia.org/wiki/Antisymmetric_relation http://en.wikipedia.org/wiki/Reflexive_relation http://en.wikipedia.org/wiki/Total_order http://stackoverflow.com/questions/6480365/comparing-arrays-in-php-interesting-behaviour/6481181#6481181 Test script: --------------- <?php $b = array("a" => 1, "b" => 2); $a = array("b" => 1, "a" => 2); var_dump($a < $b); var_dump($b < $a); var_dump($b > $a); var_dump($a > $b); Expected result: ---------------- bool(true) //or false bool(false) //or true bool(true) //or false bool(false) //or true Actual result: -------------- bool(true) bool(true) bool(true) bool(true) -- Edit bug report at https://bugs.php.net/bug.php?id=60007&edit=1 -- Try a snapshot (PHP 5.2): https://bugs.php.net/fix.php?id=60007&r=trysnapshot52 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=60007&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=60007&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=60007&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=60007&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=60007&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=60007&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=60007&r=needscript Try newer version: https://bugs.php.net/fix.php?id=60007&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=60007&r=support Expected behavior: https://bugs.php.net/fix.php?id=60007&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=60007&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=60007&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=60007&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=60007&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=60007&r=dst IIS Stability: https://bugs.php.net/fix.php?id=60007&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=60007&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=60007&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=60007&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=60007&r=mysqlcfg