gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:11:17 26 4
gpt4 key购买 nike

将 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 元素的不确定性有关。我找到了一个 research关于这个与 php 5 相关的主题。

我通过指示相对位置 msrp 解决了这个问题。但我想知道为什么它在 php5 中有效而在 php7 中无效。这些是新版php的特性还是bug?

添加 #1

问题不仅在于 PHP7 不知道如何对相等元素进行排序,例如 msrpgrand_total。如果您查看项目 shippingfreeshipping,那么它们的定义很明确,谁应该更早。 PHP5 解决了这个问题,而 PHP7 没有。

最佳答案

来自usort()文档:

Note: If two members compare as equal, their relative order in the sorted array is undefined.

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

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

关于php - 函数 uasort 在 PHP 5.5 和 PHP 7.0 中的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34281113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com