Doc #51835 [NEW]: Example cmp() in uasort() documentation is incorrect

From: Date: Sun, 16 May 2010 20:40:27 +0000
Subject: Doc #51835 [NEW]: Example cmp() in uasort() documentation is incorrect
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-4387@lists.php.net to get a copy of this message
From: Operating system: Ubuntu/9.10 PHP version: Irrelevant Package: Documentation problem Bug Type: Documentation Problem Bug description:Example cmp() in uasort() documentation is incorrect Description: ------------ The sorting function used in the example of uasort() is incorrect because it will cause elements with equal values to still be swapped. Sorting functions should never return a value of 0. It should always be either -1 or 1. Test script: --------------- This is the exact sorting example from http://php.net/manual/en/function.uasort.php: <?php // Comparison function function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } // Array to be sorted $array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -1); print_r($array); // Sort and print the resulting array uasort($array, 'cmp'); print_r($array); ?> Expected result: ---------------- To not have the 'c' and 'd' elements swapped. <?php Array ( [a] => 4 [b] => 8 [c] => -1 [d] => -1 ) Array ( [c] => -1 [d] => -1 [a] => 4 [b] => 8 ) ?> Actual result: -------------- The 'c' and 'd' elements are swapped, even though they had the same values. <?php Array ( [a] => 4 [b] => 8 [c] => -1 [d] => -1 ) Array ( [d] => -1 [c] => -1 [a] => 4 [b] => 8 ) ?> -- Edit bug report at http://bugs.php.net/bug.php?id=51835&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51835&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51835&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51835&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51835&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51835&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51835&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51835&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51835&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51835&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51835&r=support Expected behavior: http://bugs.php.net/fix.php?id=51835&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51835&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51835&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51835&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51835&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51835&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51835&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51835&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51835&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51835&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51835&r=mysqlcfg

« previous php.doc.bugs (#4387) next »