猿问

PHP:使用闭包语法“use()”的 usort

编辑:我重写了我的帖子,以便更清楚地提供一个具有实际价值的独立案例(不再有 Ajax)。


我有 2 个完全相同的数组,只是其中一个具有相同的值但已清理(html、特殊字符等)。


我想评估针对“arrayClean”的排序,但根据该评估对“arrayOriginal”进行排序(而不是 arrayClean)。


所以,这就是我所拥有的:


<?php


$arrayOriginal  = array(

    array('id' => '100','surface' => '<span>300</span>','whatever' => 'qSDqsd'),

    array('id' => '5465','surface' => '100 ch','whatever' => 'ghjkghjk'),

    array('id' => '40489','surface' => '<b>1000</b>','whatever' => 'fgsdfg')

);


$arrayClean = array(

    array('id' => '100','surface' => '300','whatever' => 'qSDqsd'),

    array('id' => '5465','surface' => '100','whatever' => 'ghjkghjk'),

    array('id' => '40489','surface' => '1000','whatever' => 'fgsdfg')

);


usort($arrayOriginal, function($a, $b) use (&$arrayClean) {

    return $a['surface'] < $b['surface'];

});


echo '<pre>'; print_r($arrayOriginal); echo '</pre>';


?>

这是我得到的(这是错误的,因为排序似乎没有考虑 arrayClean):


Array

(

[0] => Array

    (

        [id] => 100

        [surface] => <span>300</span>

        [whatever] => qSDqsd

    )


[1] => Array

    (

        [id] => 40489

        [surface] => <b>1000</b>

        [pwhatever] => fgsdfg

    )


[2] => Array

    (

        [id] => 5465

        [surface] => 100 ch

        [whatever] => ghjkghjk

    )


)

但如果我单独使用 arrayClean,只是为了检查排序脚本是否正确:


usort($arrayClean, function($a, $b) {

    return $a['surface'] < $b['surface'];

});


echo '<pre>'; print_r($arrayClean); echo '</pre>';

那么结果就是我所期望的:


Array

(

[0] => Array

    (

        [id] => 40489

        [surface] => 1000

        [whatever] => fgsdfg

    )


[1] => Array

    (

        [id] => 100

        [surface] => 300

        [whatever] => qSDqsd

    )


[2] => Array

    (

        [id] => 5465

        [surface] => 100

        [whatever] => ghjkghjk

    )


)

因此,评估 arrayClean 但相应地对 arrayOriginal 进行排序似乎不起作用。它只评估和排序 arrayOriginal。我用错了“use()”吗?我应该用别的东西吗?


慕沐林林
浏览 200回答 3
3回答

素胚勾勒不出你

假设两个数组都按公共因素(即 )排序id。uksort($arrayOriginal, function($a, $b) use ($arrayClean) {&nbsp; &nbsp; return $arrayClean[$a]['surface'] < $arrayClean[$b]['surface'];});再次强调,要使其工作,两个数组必须包含相同顺序的元素。在您的情况下,两个数组的元素必须按以下顺序(按 ID)排列:100、5465、40489但我宁愿做类似的事情:usort($arrayOriginal, function($a, $b) {&nbsp; &nbsp; return yourSurfaceCleanMethod($arrayOriginal['surface']) < yourSurfaceCleanMethod($arrayOriginal['surface']);});当然,一切都取决于您的需要,但是如果您$arrayClean仅将其用作对原始数组进行排序的参考并且您yourSurfaceCleanMethod手边有它,我肯定会执行上述操作。

哔哔one

我认为如果您的 arrayOriginal 应该根据 HTML 标记的内容进行排序,则不需要 arrayClean。strip_tags()可用于排序函数中。usort($arrayOriginal,&nbsp;function($a,&nbsp;$b)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;return&nbsp;strip_tags($b['surface'])&nbsp;<=>&nbsp;strip_tags($a['surface']); });注意:使用飞船操作符<=>得到正确的比较结果1、0和-1。

皈依舞

我礼貌地接受了 Nemoden 的回答,因为从技术上讲,它根据其范围(所提供的测试用例)回答了问题。但这是我发现最适合我的解决方案:$arrayOriginal&nbsp; = array(&nbsp; &nbsp; array('id' => '100','surface' => '<span>300</span>','whatever' => 'qSDqsd'),&nbsp; &nbsp; array('id' => '5465','surface' => '100 ch','whatever' => 'ghjkghjk'),&nbsp; &nbsp; array('id' => '40489','surface' => '<b>1000</b>','whatever' => 'fgsdfg'));$arrayClean = array(&nbsp; &nbsp; array('id' => '100','surface' => '300','whatever' => 'qSDqsd'),&nbsp; &nbsp; array('id' => '5465','surface' => '100','whatever' => 'ghjkghjk'),&nbsp; &nbsp; array('id' => '40489','surface' => '1000','whatever' => 'fgsdfg'));$surface = array_column($arrayClean, 'Surface');array_multisort($surface, SORT_DESC, SORT_REGULAR, $arrayOriginal);然后我得到这个输出,这正是我想要的:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 40489&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [surface] => <b>1000</b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [whatever] => fgsdfg&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 100&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [surface] => <span>300</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [whatever] => qSDqsd&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 5465&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [surface] => 100 ch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [whatever] => ghjkghjk&nbsp; &nbsp; &nbsp; &nbsp; ))Nemoden 的回答非常有帮助,但无论出于何种原因,它给了我这个:Array(&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 40489&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [surface] => <b>1000</b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [whatever] => fgsdfg&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 100&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [surface] => <span>300</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [whatever] => qSDqsd&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 5465&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [surface] => 100 ch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [whatever] => ghjkghjk&nbsp; &nbsp; &nbsp; &nbsp; ))&nbsp; &nbsp;子数组以正确的顺序打印,但子数组索引未更改。因此,当导出到 javascript“arrayOriginal”时,它会回到其初始排序,因为子数组将再次根据它们的索引重新排序。至少,我是这样经历的。不管怎样,现在问题已经解决了,我在这里得到的答案非常有帮助。
随时随地看视频慕课网APP
我要回答