使用 usort ABC 值对 PHP 阿雷进行排序

请帮助完成以下示例代码。


我正在尝试进行排序,使“B”在数组中排名最后。


我已经尝试了很多方法。最初我和乌索特一起工作。但是为了调试,我已将其简化为usort。


<?php

$array = json_decode('[

    {

        "id": "222",

        "name": "A",

        "shouldgolast": "N"

    },

    {

        "id": "261",

        "name": "B",

        "shouldgolast": "Y"

    },

    {

        "id": "262",

        "name": "C",

        "shouldgolast": "N"

    }

]', true);

usort($array, "sortF");

foreach ($array as $details) {

    echo $details["name"]."\n";

}

function sortF($a, $b)

{

    if($a['shouldgolast'] == "Y"){

        return 1;

    }else if($b['shouldgolast'] == "Y"){

        return 1;

    }

    return 0;

}

?>

上面的输出是


B

A

C

我希望B是最后一个。


测试链接: https://www.tehplayground.com/DzqhjFq6lK9Zq9Ue


也尝试返回 -1 而不是 1(使 B 到第二个位置) 测试链接 2: https://www.tehplayground.com/ea51PWdFfkAupEsd


小唯快跑啊
浏览 132回答 4
4回答

料青山看我应如是

如果您只想将所有 s 移到末尾,则实际上不需要与 进行比较,甚至不需要返回零或 -1。比较表达式真正需要的唯一逻辑是“如果$a应该排在最后,请将其向下移动。shouldgolast$a$busort($array,&nbsp;fn($a)&nbsp;=>&nbsp;$a['shouldgolast']&nbsp;===&nbsp;'Y');

蝴蝶不菲

usort 函数作为第二个参数接收一个应用作比较器的可调用参数。比较器通常以这种方式工作:返回 1 ->第一个元素大于第二个元素返回 -1 ->第 2 个元素大于第一个元素返回 0 ->两个元素相同您必须以这种方式校正比较器功能function sortF($a, $b){&nbsp; &nbsp; if($a['shouldgolast'] == "Y"){&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }else if($b['shouldgolast'] == "Y"){&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}

慕村9548890

问题是你有返回的语句,我认为逻辑不适合这种情况,它应该根据可以应用于你的情况的条件返回适当的值,你的代码应该看起来像这样:else if1<?php$array = json_decode('[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "shouldgolast": "222",&nbsp; &nbsp; &nbsp; &nbsp; "name": "A",&nbsp; &nbsp; &nbsp; &nbsp; "shouldgolast": "N"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": "261",&nbsp; &nbsp; &nbsp; &nbsp; "name": "B",&nbsp; &nbsp; &nbsp; &nbsp; "shouldgolast": "Y"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": "262",&nbsp; &nbsp; &nbsp; &nbsp; "name": "C",&nbsp; &nbsp; &nbsp; &nbsp; "shouldgolast": "N"&nbsp; &nbsp; }]', true);usort($array, "sortF");foreach ($array as $details) {&nbsp; &nbsp; echo $details["name"]."\n";}function sortF($a, $b){&nbsp; &nbsp; if($a['shouldgolast'] == "Y"){&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }&nbsp; &nbsp; if($b['shouldgolast'] == "Y"){&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}?>

萧十郎

您无需对值进行排序。方法#1:您只需使用指针即可将所有数组移动到右侧。这类似于荷兰国旗算法,我们将项目分组在一起。shouldgolastY片段:$yes_ptr = count($array) - 1;for($i = count($array) - 1; $i >= 0; -- $i){&nbsp; &nbsp; if($array[$i]['shouldgolast'] == 'Y'){&nbsp; &nbsp; &nbsp; &nbsp; $temp = $array[$yes_ptr];&nbsp; &nbsp; &nbsp; &nbsp; $array[$yes_ptr--] = $array[$i];&nbsp; &nbsp; &nbsp; &nbsp; $array[$i] = $temp;&nbsp; &nbsp; }}演示:https://3v4l.org/KHFQr在上面的方法中,我们只保留一个指针,该指针指示当前位置,其中应插入任何新的即将到来。请注意,这将首先成功对所有内容进行分组,稍后再对所有内容进行分组。但是,这可能会更改具有值 的元素的内部顺序。yes_ptrYNYN方法#2:如果你想要一个稳定的分组,意味着保持所有元素和元素的顺序,你可以简单地将它们收集在2个不同的数组中,并做一个array_merge。NY片段:$no_array = [];$yes_array = [];foreach($array as $value){&nbsp; &nbsp; if($value['shouldgolast'] == 'N'){&nbsp; &nbsp; &nbsp; &nbsp; $no_array[] = $value;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $yes_array[] = $value;&nbsp; &nbsp; }}print_r(array_merge($no_array,$yes_array));演示:https://3v4l.org/Se1qd
打开App,查看更多内容
随时随地看视频慕课网APP