函数uasort在PHP 5.5和PHP 7.0中的不同行为
将php版本从5.5更改为7.0后,我遇到了Magento 1.8的奇怪行为.
这个奇怪的行为是由于工作功能的改变而引起的. 源代码: <?php $arr = [ "nominal" => [ "before" => ["subtotal","grand_total"],"after" => [],"_code" => "nominal" ],"subtotal" => [ "after" => ["nominal"],"before" => ["grand_total","shipping","freeshipping","tax_subtotal","discount","tax","weee"],"_code" => "subtotal" ],"shipping" => [ "after" => ["subtotal","nominal","tax_shipping","tax"],"_code" => "shipping" ],"grand_total" => [ "after" => ["subtotal","before" => [],"_code" => "grand_total" ],"msrp" => [ "before" => [],"_code" => "msrp" ],"freeshipping" => [ "after" => ["subtotal","nominal"],"before" => ["tax_subtotal","grand_total","discount"],"_code" => "freeshipping" ],"discount" => [ "after" => ["subtotal","_code" => "discount" ],"tax_subtotal" => [ "after" => ["0" => "freeshipping","1" => "subtotal","3" => "nominal"],"before" => ["tax","_code" => "tax_subtotal" ],"tax_shipping" => [ "after" => ["shipping","subtotal","_code" => "tax_shipping" ],"tax" => [ "after" => ["subtotal","before" => ["grand_total"],"_code" => "tax" ],"weee" => [ "after" => ["subtotal","freeshipping"],"before" => ["shipping","tax_shipping"],"_code" => "weee" ] ]; function _compareTotals($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; } echo sprintf("%s <> %s: %s",$aCode,$bCode,$res) . "n"; return $res; } uasort($arr,'_compareTotals'); var_dump(array_keys($arr)); 在php 5.5结果是: freeshipping <> subtotal: 1 freeshipping <> shipping: -1 weee <> freeshipping: 1 tax <> freeshipping: 1 tax_shipping <> freeshipping: 1 tax_subtotal <> freeshipping: 1 discount <> freeshipping: 1 nominal <> freeshipping: -1 freeshipping <> grand_total: -1 msrp <> freeshipping: 0 subtotal <> msrp: 0 nominal <> subtotal: -1 tax_subtotal <> shipping: -1 weee <> tax_subtotal: 1 tax <> tax_subtotal: 1 tax_shipping <> tax_subtotal: 1 grand_total <> tax_subtotal: 1 discount <> tax_subtotal: 1 shipping <> tax_subtotal: 1 grand_total <> discount: 1 grand_total <> shipping: 1 grand_total <> tax_shipping: 1 grand_total <> tax: 1 weee <> grand_total: -1 shipping <> discount: -1 tax <> shipping: 1 tax_shipping <> shipping: 1 weee <> shipping: -1 tax_shipping <> discount: -1 tax <> tax_shipping: 1 discount <> tax_shipping: 1 tax <> discount: 1 array(11) { [0] => string(7) "nominal" [1] => string(8) "subtotal" [2] => string(4) "msrp" [3] => string(12) "freeshipping" [4] => string(12) "tax_subtotal" [5] => string(4) "weee" [6] => string(8) "shipping" [7] => string(12) "tax_shipping" [8] => string(8) "discount" [9] => string(3) "tax" [10] => string(11) "grand_total" } 在php 7.0中的结果是: nominal <> subtotal: -1 subtotal <> shipping: -1 shipping <> grand_total: -1 grand_total <> msrp: 0 msrp <> freeshipping: 0 freeshipping <> discount: -1 discount <> tax_subtotal: 1 msrp <> tax_subtotal: 0 freeshipping <> tax_subtotal: -1 discount <> tax_shipping: 1 freeshipping <> tax_shipping: -1 tax_subtotal <> tax_shipping: -1 discount <> tax: -1 tax <> weee: 1 tax_shipping <> weee: 1 freeshipping <> weee: -1 tax_subtotal <> weee: -1 array(11) { [0] => string(7) "nominal" [1] => string(8) "subtotal" [2] => string(8) "shipping" [3] => string(11) "grand_total" [4] => string(4) "msrp" [5] => string(12) "freeshipping" [6] => string(12) "tax_subtotal" [7] => string(4) "weee" [8] => string(12) "tax_shipping" [9] => string(8) "discount" [10] => string(3) "tax" } 在PHP5中,grand_total是最后一个元素,但是在PHP7 – 否. 我通过指示一个相对位置msrp解决了这个问题.但是我不知道为什么它在php5中工作,在php7中不起作用.这些是新版php或bug的功能? 加1# 问题不仅在于PHP7不知道如何排序相等的元素,例如msrp和grand_total.
从
usort() 文档:
这是你在这里看到的. PHP 7使用不同的,部分稳定的排序算法,因此根据排序功能比较相等的元素现在可能有不同的顺序. 如果您关心相等元素的排序顺序(这不仅仅是一个测试问题),那么您应该在比较函数中显式显示. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |