【发布时间】:2016-11-22 13:19:40
【问题描述】:
我需要合并并排序两个具有不同数据结构的数组(无法在 MySQL 查询中排序),但两者都有一个 created_on 字段。
所以我将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() 是否可行,因为当您调用该函数时,它根本不带参数?还有其他选择吗?
【问题讨论】: