我有保存對象的屬性$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"
}
....
我是間在任何解決方案,即使我必須創建一個新的數組,我可以排序後,等等
螞蟻的想法?謝謝
做到了。謝謝 – Patrioticcow
我如何在控制器中使用它。如果我創建一個'公共函數cmp($ a,$ b)',然後用'usort($ this-> result,$ this-> cmp())'調用它,我會得到錯誤,因爲我需要通過'$ a'和'$ b'方法 – Patrioticcow
@Patrioticcow你是如何在第一個地方得到對象的 – Baba