Bug #66544 [Com]: uasort works incorrent in some cases

From: Date: Wed, 22 Jan 2014 18:38:09 +0000
Subject: Bug #66544 [Com]: uasort works incorrent in some cases
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183955@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66544&edit=1 ID: 66544 Comment by: mail at requinix dot net Reported by: korovkin dot kirill at gmail dot com Summary: uasort works incorrent in some cases Status: Open Type: Bug Package: *General Issues Operating System: Linux 2.6.32-042stab07 PHP Version: 5.4.24 Block user comment: N Private report: N New Comment: Looks like undefined behavior to me: your comparison function claims that discount == freeshipping and tax_shipping == freeshipping, yet tax_shipping < discount. Pick an unambiguous place for freeshipping to go, update the arrays so that your function can definitively state the sort order for every pairing, and it will sort correctly. Besides, allowing freeshipping to land wherever the sort happens to place it doesn't sound like a good idea. Previous Comments: ------------------------------------------------------------------------ [2014-01-22 14:11:34] korovkin dot kirill at gmail dot com Description: ------------ You can see all information in the test script. Test script: --------------- $inputArray = array( 'shipping' => array( 'after' => array(), 'before' => array(), '_code' => 'shipping' ), 'freeshipping' => array( 'after' => array(), 'before' => array(), '_code' => 'freeshipping' ), 'discount' => array( 'after' => array('shipping', 'tax_shipping'), 'before' => array(), '_code' => 'discount' ), 'tax_shipping' => array( 'after' => array('shipping'), 'before' => array('discount'), '_code' => 'tax_shipping' ) ); function compare ($a, $b) { $aCode = $a['_code']; $bCode = $b['_code']; if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) { $res = -1; } elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) { $res = 1; } else { $res = 0; } return $res; } uasort($inputArray, 'compare'); var_dump(array_keys($inputArray)); Expected result: ---------------- array (size=4) 0 => string 'shipping' (length=8) 1 => string 'tax_shipping' (length=12) 2 => string 'discount' (length=8) 3 => string 'freeshipping' (length=12) Or another position for 'freeshipping'. But 'shipping' must be before 'tax_shipping' and 'discount' must be after 'tax_shipping'. Actual result: -------------- array (size=4) 0 => string 'tax_shipping' (length=12) 1 => string 'discount' (length=8) 2 => string 'freeshipping' (length=12) 3 => string 'shipping' (length=8) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66544&edit=1

« previous php.bugs (#183955) next »