Using usort in php with a class private function

后端 未结 5 970
闹比i
闹比i 2020-12-02 07:10

ok using usort with a function is not so complicated

This is what i had before in my linear code

function merchantSort($a,$b){
    return ....// stuf         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 07:52

    In this example I am sorting by a field inside the array called AverageVote.

    You could include the method inside the call, which means you no longer have the class scope problem, like this...

            usort($firstArray, function ($a, $b) {
               if ($a['AverageVote'] == $b['AverageVote']) {
                   return 0;
               }
    
               return ($a['AverageVote'] < $b['AverageVote']) ? -1 : 1;
            });
    

提交回复
热议问题