可能重复: PHP custom sorting
我有一个数组,
Array
(
[Flag] => 2
[Pending] => 11
[Received] => 11
[Sent] => 8
[Skip] => 5
[Complaint] => 1
[Query] => 1
)
我对上述数组有两个请求:
如何将此排序为已接收,已发送,待处理,举报,跳过,查询,请求,投诉。
正如我们在阵列中看到“请求”不可用,如何将其推入数组为零的数组?
答案 0 :(得分:1)
$array = array
(
"Flag" => 2
"Pending" => 11
"Received" => 11
"Sent" => 8
"Skip" => 5
"Complaint" => 1
"Query" => 1
);
$array["Request"] = 0;
function my_sort($a, $b) {
$order = array("Received", "Sent", "Pending", "Flag", "Skip", "Query", "Request", "Complaint");
return array_search($b, $order) - array_search($a, $order);
}
uksort($array, "my_sort");
print_r($array);
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试usort,然后是
$array['Request'] = 0;