我需要合併,然後排序具有不同數據結構的兩個數組(不能在MySQL查詢中排序),但其中都有created_on
字段。更好的PHP usort()
所以我使用usort()
自定義函數。
在我的控制器
usort(merged_array, 'sort_records');
在我的輔助功能
if(!function_exists('sort_records')){
function sort_records($a,$b){
if ($a['created_at'] == $b['created_at'])
return 0;
if ($a['created_at'] < $b['created_at'])
return -1;
return 1;
}
}
我想使這個sort_records()
功能重用。所以我可以在其他數組中使用它。也許是這樣..
function sort_records($a,$b,$index){
if ($a[$index] == $b[$index])
return 0;
if ($a[$index] < $b[$index])
return -1;
return 1;
這可能與usort()
當你調用不帶參數的所有功能,因爲?還有其他選擇嗎?
其實我喜歡這個有很多,但其他的答案之一是我當前的應用程序更好地調用它。 – skribe