usort issue with decimal numbers

前端 未结 3 2035
醉酒成梦
醉酒成梦 2020-12-05 11:41

I\'m currently trying to sort a multidimensional array by its subvalues. The structure of the array is:

[0] => Array
    (
        [id] => 87
        [         


        
3条回答
  •  一生所求
    2020-12-05 12:46

    See usort docs. Float result will be converted to integer. For correct work use this code:

    usort(
        $data, 
        function($a, $b) {
            $result = 0;
            if ($a[$_GET['sortby']] > $b[$_GET['sortby']]) {
                $result = 1;
            } else if ($a[$_GET['sortby']] < $b[$_GET['sortby']]) {
                $result = -1;
            }
            return $result; 
        }
    );
    

提交回复
热议问题