Php数组按日期下降合并和排序
问题描述:
I need to combine 2 arrays coming from data base (each one ordered by date descent) and echo a new one together by date descent. Studying php sort functions I got to this code:
//Function
function dateSort($a,$b){
$dateA = strtotime($a['data']);
$dateB = $b['payment_date'];//already unixtime
return ($dateA-$dateB);
}
// Merge the arrays
$h_pp_ps = array_merge($h_pp,$h_ps);
// Sort the array using the call back function
usort($h_pp_ps, 'dateSort');
//PRINT!!
print_r($h_pp_ps);
This will produce results from low date to high.... how to get it from high to low?
我需要组合2个来自数据库的数组(每个数据按日期下降排序)并一起回显一个新数组 按日期下降。 学习php排序函数我得到了这段代码: p>
// Function
function dateSort($ a,$ b){
$ dateA = strtotime($ a ['data ']);
$ dateB = $ b ['payment_date']; //已经是unixtime
return($ dateA- $ dateB);
}
//合并数组
$ h_pp_ps = array_merge( $ h_pp,$ h_ps);
//使用回调函数
nortort($ h_pp_ps,'dateSort')排序数组;
// PRINT !!
print_r($ h_pp_ps);
code> pre>
这会产生从低日期到高日的结果....如何从高到低? p>
div>
答
Nothing easier:
$h_pp_ps = array_reverse($h_pp_ps);
答
Subtract $dateA from $dateB such as ($dateB - $dateA) in the return statement of method 'dataSort', it will reverse the sorting order.
Details:
Change this method
function dateSort($a,$b){
$dateA = strtotime($a['data']);
$dateB = $b['payment_date'];//already unixtime
return ($dateA-$dateB);
}
To:
function dateSort($a,$b){
$dateA = strtotime($a['data']);
$dateB = $b['payment_date'];//already unixtime
return ($dateB - $dateA);
}