我需要合并,然后排序具有不同数据结构的两个数组(不能在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