To sort multidimensional array by values, you can use a user-defined comparison function usort. The function sort array by its values using a user-defined comparison function. If you want to sort an array by some non-trivial criteria, you should use this function.
usort ( array &$array , callback $cmp_function )
For example we have an array to sort:
$array[0]["time"] = "10.05"; $array[1]["time"] = "09.50"; $array[2]["time"] = "10.00"; $array[3]["time"] = "10.00"; $array[4]["time"] = "11.00";
We need to sort this array according to the time in ascending order. So we need to use a user-defined comparison function to sort this type of array.
function compare_multi($a, $b) { if($a['time'] < $b['time']){ return -1; }else if($a['time'] > $b['time']){ return 1; }else if($a['time'] == $b['time']){ return 0; } } usort($array, "compare_multi");
It will give you the sorted array but assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.
If you are using a class you should use it like as below:
usort($array, array('ClassName','compare_multi'));
OR
usort($array, array($this,'compare_multi'));
If you want the sort array result in descending order you need to change a little in comparison function as below:
function compare_multi($a, $b) { if($a['time'] > $b['time']){ return -1; }else if($a['time'] < $b['time']){ return 1; }else if($a['time'] == $b['time']){ return 0; } } usort($array, "compare_multi");
Hope this will help you!