2012-12-05 38 views
1

我有保存對象的屬性$this->result如何在PHP中對對象進行排序?

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" 
    } 
.... 

我是間在任何解決方案,即使我必須創建一個新的數組,我可以排序後,等等

螞蟻的想法?謝謝

回答

4

你所追求的是usort。

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"); 
+0

做到了。謝謝 – Patrioticcow

+0

我如何在控制器中使用它。如果我創建一個'公共函數cmp($ a,$ b)',然後用'usort($ this-> result,$ this-> cmp())'調用它,我會得到錯誤,因爲我需要通過'$ a'和'$ b'方法 – Patrioticcow

+0

@Patrioticcow你是如何在第一個地方得到對象的 – Baba

2

我可以看到你有超過40 Images,它仍然可以增長等..而且我不知道你在哪裏得到它,但可以只使用堆儲存圖像,它會自動排序...

$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; 
    } 
} 
+1

+1因爲SPL規則 – ficuscr

相關問題