Sort Object in PHP

后端 未结 10 1411
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 01:51

What is an elegant way to sort objects in PHP? I would love to accomplish something similar to this.

$sortedObjectArary = sort($unsortedObjectArray, $Object-         


        
10条回答
  •  盖世英雄少女心
    2020-11-29 02:30

    Almost verbatim from the manual:

    function compare_weights($a, $b) { 
        if($a->weight == $b->weight) {
            return 0;
        } 
        return ($a->weight < $b->weight) ? -1 : 1;
    } 
    
    usort($unsortedObjectArray, 'compare_weights');
    

    If you want objects to be able to sort themselves, see example 3 here: http://php.net/usort

提交回复
热议问题