【问题标题】:how to sort an object in php?如何在php中对对象进行排序?
【发布时间】:2012-12-05 20:56:22
【问题描述】:

我有一个属性$this->result 包含一个对象:

array(40) {
  [0] => object(Model)#181 (7) {
    ["_id":protected] => string(2) "1"
    ["_img":protected] => string(95) "/1273720855.jpg"
  }
  [1] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "2"
    ["_img":protected] => string(95) "/test.jpg"
  }
  [2] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "3"
    ["_img":protected] => string(95) "/127377775.jpg"
  }
  [3] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "4"
    ["_img":protected] => string(95) "/test.jpg"
  }
  [4] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "5"
    ["_img":protected] => string(95) "/129586775.jpg"
  }
...

所以如果我做一个循环,我可以获得img 属性:

foreach($this->result as $val){
    echo $val->getImg(); //'/1273720855.jpg'
}

我想做的是对对象进行排序,以使具有/test.jpg 的属性位于最后或最后回显它们,例如:

array(40) {
  [2] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "3"
    ["_img":protected] => string(95) "/127377775.jpg"
  }
  [4] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "5"
    ["_img":protected] => string(95) "/129586775.jpg"
  }
  [1] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "2"
    ["_img":protected] => string(95) "/test.jpg"
  } 
  [3] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "4"
    ["_img":protected] => string(95) "/test.jpg"
  }
....

我对任何解决方案都感兴趣,即使我必须创建一个可以对后缀进行排序的新数组等

蚂蚁的想法?谢谢

【问题讨论】:

    标签: php arrays zend-framework sorting object


    【解决方案1】:

    你所追求的是优胜劣汰。

    http://php.net/usort

    bool usort ( array &$array , callable $cmp_function )
    

    您编写函数来运行排序,并将此函数作为参数 2 传递。 然后 PHP 将遍历数组并针对数组中的每个值运行该函数。

    所以你应该有类似这样的东西:

    <?php
    function cmp($a, $b)
    {
        return strcmp($a->getImg(), $b->getImg());
    }
    
    usort($this->result, "cmp");
    

    【讨论】:

    • 如何在控制器中使用它。如果我创建一个public function cmp($a, $b) 然后在使用usort($this-&gt;result, $this-&gt;cmp()) 的操作中调用它,我会收到错误,因为我需要将$a$b 传递给该方法
    • @Patrioticcow 你是怎么得到这个对象的
    • 在我使用fetchAll的另一种方法中,查询一些结果
    • 那很好 .. 你考虑过使用 SplHeapMax 代替吗?
    • $a 和 $b 由 PHP 自动传递。基本上,它运行数组中数据的冒泡排序,并将 1 与 2、1 与 3 (等等)进行比较,直到找到正确的位置。
    【解决方案2】:

    我可以看到你有超过 40 Images 并且它仍然可以增长等等......我不确定你从哪里得到它但可以使用 Heap 来存储你的图像并且它会自动排序......

    $heap = new ImageStorage();
    
    
    // Porpulate Storage form source 
    $images = array(1 => "/1273720855.jpg",2 => "/test.jpg",3 => "/127377775.jpg",4 => "/test.jpg",5 => "/129586775.jpg");
    foreach ( $images as $id => $img ) {
        $heap->insert(new Model($id, $img));
    }
    
    // Simple Output
    echo "<pre>";
    foreach ( $heap as $img ) {
        echo $img->getImg(), PHP_EOL;
    }
    

    输出

    /1273720855.jpg
    /127377775.jpg
    /129586775.jpg
    /test.jpg
    /test.jpg
    

    使用的类

    // Image Sotrage Class
    class ImageStorage extends SplHeap {
        public function compare($a, $b) {
            return strcmp($b->getImg(), $a->getImg());
        }
    }
    
    // Emulation of your Model class
    class Model {
        protected $_id;
        protected $_img;
        function __construct($id, $img) {
            $this->_id = $id;
            $this->_img = $img;
        }
    
        public function getID() {
            return $this->_id;
        }
    
        public function getImg() {
            return $this->_img;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多