请帮助完成以下示例代码。
我正在尝试进行排序,使“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
料青山看我应如是
蝴蝶不菲
慕村9548890
萧十郎