php7与php5函数区别,函数uasort在PHP 5.5和PHP 7.0中的不同行为

将php版本从5.5更改为7.0后,我遇到了Magento 1.8的奇怪行为.

这个奇怪的行为是由于工作功能的改变而引起的.

源代码:

$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不知道如何排序相等的元素,例如msrp和grand_total.

如果你看货物运输和发货,那么他们是明确的,谁应该更早. PHP5解决了这个问题,而PHP7没有.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值