PHP5.5和PHP7.0中uasort函数的不同行为

PHP5.5和PHP7.0中uasort函数的不同行为,php,magento,php-5.5,php-7,Php,Magento,Php 5.5,Php 7,在将php版本从5.5更改为7.0后,我遇到了Magento1.8的一个奇怪行为。 这种奇怪的行为是由于工作函数uasort发生了变化 源代码: <?php $arr = [ "nominal" => [ "before" => ["subtotal", "grand_total"], "after" => [], "_code" => "nominal" ], "subtotal" =>

在将php版本从5.5更改为7.0后,我遇到了Magento1.8的一个奇怪行为。 这种奇怪的行为是由于工作函数
uasort
发生了变化

源代码:

<?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", "freeshipping", "tax_subtotal", "nominal", "weee"],
        "before" => ["grand_total", "discount", "tax_shipping", "tax"],
        "_code" => "shipping"
    ],
    "grand_total" => [
        "after" => ["subtotal", "nominal", "shipping", "freeshipping", "tax_subtotal", "discount", "tax"],
        "before" => [],
        "_code" => "grand_total"
    ],
    "msrp" => [
        "before" => [],
        "after" => [],
        "_code" => "msrp"
    ],
    "freeshipping" => [
        "after" => ["subtotal", "nominal"],
        "before" => ["tax_subtotal", "shipping", "grand_total", "tax", "discount"],
        "_code" => "freeshipping"
    ],
    "discount" => [
        "after" => ["subtotal", "shipping", "nominal", "freeshipping", "tax_subtotal", "tax_shipping", "weee"],
        "before" => ["grand_total", "tax"],
        "_code" => "discount"
    ],
    "tax_subtotal" => [
        "after" => ["0" => "freeshipping", "1" => "subtotal", "3" => "nominal"],
        "before" => ["tax", "discount", "shipping", "grand_total", "tax_shipping", "weee"],
        "_code" => "tax_subtotal"
    ],
    "tax_shipping" => [
        "after" => ["shipping", "tax_subtotal", "subtotal", "freeshipping", "nominal"],
        "before" => ["tax", "discount", "grand_total"],
        "_code" => "tax_shipping"
    ],
    "tax" => [
        "after" => ["subtotal", "shipping", "discount", "tax_subtotal", "freeshipping", "tax_shipping", "nominal", "weee"],
        "before" => ["grand_total"],
        "_code" => "tax"
    ],
    "weee" => [
        "after" => ["subtotal", "tax_subtotal", "nominal", "freeshipping"],
        "before" => ["shipping", "tax", "discount", "grand_total", "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));
文档中的

注意:如果两个成员比较相等,则它们在排序数组中的相对顺序未定义

这就是你在这里看到的。PHP7使用一种不同的、部分稳定的排序算法,因此根据排序函数比较相等的元素现在可能具有不同的顺序

如果您关心相等元素的排序顺序(这不仅仅是一个测试问题),那么您应该在比较函数中明确它。

可能的解决方案 我创建了一个magento模块,用于解决php7的magento总计计算问题。 我遇到的问题是,amazonpayments结帐页面上的模块总计增加了两次税款

信用 解决方案是由解决了我的问题-因此这只是打包到模块中的修复

模块:

在PHP7中工作时,只需按以下方式调用函数:

uasort($result, function ($a, $b) {
   your_sort_function($a, $b);   //call your sort function here
});
因此,请修改您的代码,如下所示:

function\u比较总计($a,$b)
{
$aCode=$a[“U代码];
$b代码=$b[“U代码”];
if(在_数组中($aCode,$b['after'])|在_数组中($bCode,$a['before'])){
$res=-1;
}elseif(在_数组中($bCode,$a['after'])|在_数组中($aCode,$b['before'])){
$res=1;
}否则{
$res=0;
}
echo sprintf(“%s%s:%s”,$aCode,$bCode,$res)。“\n”;
返回$res;
}
uasort($arr,函数($a,$b){
_比较总计(a美元、b美元);
});
变量转储(数组密钥($arr));

有趣的是,PHP7只需较少的步骤即可完成排序;我想知道这是否是巧合,或者修改后的算法对于此类数据是否通常更有效。这独立于PHP 7,并且早在2012年就有过报道:,给出的内部Magento票证是[MCACE-129]。@hakre,是,问题不仅在于PHP7不知道如何对相等的元素进行排序,例如
msrp
grand_total
。如果您查看项目
配送
免费配送
,那么它们都有明确的定义,谁应该更早。PHP5解决了这个问题,而PHP7却没有。@danil,如果你说有些东西是相等的,而有些东西是不相等的,PHP就不能正确地对数组排序。因为您的函数对某些不相等的内容返回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"
}
uasort($result, function ($a, $b) {
   your_sort_function($a, $b);   //call your sort function here
});
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, function($a, $b) {
    _compareTotals($a, $b);
});
var_dump(array_keys($arr));