Sort Object in PHP

后端 未结 10 1386
被撕碎了的回忆
被撕碎了的回忆 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:21

    For php >= 5.3

    function osort(&$array, $prop)
    {
        usort($array, function($a, $b) use ($prop) {
            return $a->$prop > $b->$prop ? 1 : -1;
        }); 
    }
    

    Note that this uses Anonymous functions / closures. Might find reviewing the php docs on that useful.

提交回复
热议问题