Sort Object in PHP

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

    0 讨论(0)
  • 2020-11-29 02:28

    You could use the usort() function and make your own comparison function.

    $sortedObjectArray = usort($unsortedObjectArray, 'sort_by_weight');
    
    function sort_by_weight($a, $b) {
        if ($a->weight == $b->weight) {
            return 0;
        } else if ($a->weight < $b->weight) {
            return -1;
        } else {
            return 1;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:29

    You can even build the sorting behavior into the class you're sorting, if you want that level of control

    class thingy
    {
        public $prop1;
        public $prop2;
    
        static $sortKey;
    
        public function __construct( $prop1, $prop2 )
        {
            $this->prop1 = $prop1;
            $this->prop2 = $prop2;
        }
    
        public static function sorter( $a, $b )
        {
            return strcasecmp( $a->{self::$sortKey}, $b->{self::$sortKey} );
        }
    
        public static function sortByProp( &$collection, $prop )
        {
            self::$sortKey = $prop;
            usort( $collection, array( __CLASS__, 'sorter' ) );
        }
    
    }
    
    $thingies = array(
            new thingy( 'red', 'blue' )
        ,   new thingy( 'apple', 'orange' )
        ,   new thingy( 'black', 'white' )
        ,   new thingy( 'democrat', 'republican' )
    );
    
    print_r( $thingies );
    
    thingy::sortByProp( $thingies, 'prop1' );
    
    print_r( $thingies );
    
    thingy::sortByProp( $thingies, 'prop2' );
    
    print_r( $thingies );
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-29 02:31

    If you want to explore the full (terrifying) extent of lambda style functions in PHP, see: http://docs.php.net/manual/en/function.create-function.php

    0 讨论(0)
  • 2020-11-29 02:32
    function PHPArrayObjectSorter($array,$sortBy,$direction='asc')
    {
        $sortedArray=array();
        $tmpArray=array();
        foreach($this->$array as $obj)
        {
            $tmpArray[]=$obj->$sortBy;
        }
        if($direction=='asc'){
            asort($tmpArray);
        }else{
            arsort($tmpArray);
        }
    
        foreach($tmpArray as $k=>$tmp){
            $sortedArray[]=$array[$k];
        }
    
        return $sortedArray;
    
    }
    

    e.g =>

    $myAscSortedArrayObject=PHPArrayObjectSorter($unsortedarray,$totalMarks,'asc');
    
    $myDescSortedArrayObject=PHPArrayObjectSorter($unsortedarray,$totalMarks,'desc');
    
    0 讨论(0)
提交回复
热议问题