PHP 5.5和PHP 7.0中函数uasort的不同行为

13
我在将php版本从5.5改为7.0后,遇到了Magento 1.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));

在 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元素的位置不确定有关。我在这个链接中找到了一篇与php 5相关的研究文章。
我通过指定msrp的相对位置来解决这个问题。但我想知道为什么它在php5中有效,在php7中无法使用。这是新版本php的特性还是一个错误? 添加 #1 问题不仅仅在于PHP7不知道如何排序相同的元素,例如msrpgrand_total。如果您查看shippingfreeshipping项目,则可以清楚地确定谁应该更早。PHP5解决了这个问题,而PHP7没有。

有趣的是,PHP7在更少的步骤中完成了排序;我想知道这是巧合还是修订后的算法对这种数据通常更有效率。 - IMSoP
这与PHP 7无关,并且早在2012年就已经有报道了:排序算法:Magento结帐总额排序错误导致运费税计算错误,内部Magento工单号为[MCACE-129]。 - hakre
@hakre,是的,我在我的问题中写了关于它的内容。 - danil
3个回答

12

来自usort()文档:

注意:如果两个元素相等,则它们在排序后的数组中的相对顺序是未定义的。

这就是您在此处看到的情况。PHP 7使用不同的、部分稳定的排序算法,因此根据您的排序函数进行比较相等的元素可能会有不同的顺序。

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


问题不仅在于PHP7不知道如何对等元素进行排序,例如msrp和grand_total。 如果您查看shipping和freeshipping项目,则可以明确定义谁应该先。 PHP5解决了这个问题,而PHP7没有。 - danil
2
@danil,如果你说一些东西相等,但实际上它们不相等,PHP 将无法正确排序你的数组。因为你的函数对于某些不相等的元素返回了0,这会破坏其余的排序结果。 - Andrea
2
这本质上是GIGO原则的一个例子:如果你给排序函数无意义的东西,你得到的就是无意义的东西。 - Andrea
这个在升级的发布说明或文档中有明确提到吗?我找了但没有找到。这导致了一些非常令人困惑的故障排除。 - ChadSikorra

4

0

在PHP7中工作,您只需要这样调用函数:

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));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接