Possible Duplicate: PHP custom sorting
I have an array,
Array
(
[Flag] => 2
[Pending] => 11
[Received] => 11
[Sent] => 8
[Skip] => 5
[Complaint] => 1
[Query] => 1
)
I have two requests for the above array:
How can I sort this into the order Received, Sent, Pending, Flag, Skip, Query, Request, Complaint.
As we see that "Request" is not available in the array, how do I push it into the array with the value zero?
$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);
With the PHP function usort(), you can create a user-defined function and sort your array based on the results of this function.
Try usort, followed by,
$array['Request'] = 0;