0
我試圖找到一個深刻的對象項目排序一個陣列的有效方式,這裏是例子:訂購一個數組持有對象的數據對象
陣列方案:
array
[0] = animal
count->1
name->tiger
[1] = animal
count->3
name->lion
[2] = animal
count->4
name->monkey
[3] = animal
count->2
name->elephant
所需的結果降序:
array
[2] = animal
count->4
name->monkey
[1] = animal
count->3
name->lion
[3] = animal
count->2
name->elephant
[0] = animal
count->1
name->tiger
任何人都可以做得比這更好嗎?或者可能改進它?
function order_by_count($animals){
$animals_order = array();
foreach($animals as $key => $animal){
$animals_order[$key] = $animal->count;
}
natsort($animals_order);
$master_animals_order = array_reverse(array_keys($animals_order));
$revised_animals = array();
foreach($master_animals_order as $key){
$animal = $animals[$key];
$revised_animals[] = $animal;
}
return $revised_animals;
}
您有沒有看過[''usort'](http://au2.php.net/manual/en/function.usort.php)? – Howard