I have an array in following format:
array(
[0]=> array(
[0]=> array (
["ID"]=> 118
["post_date"]=> "2014-04-28 07:27:37"
["post_title"]=> "Title 1"
)
[1]=> array (
["ID"]=> 119
["post_date"]=> "2014-04-29 07:27:37"
["post_title"]=> "title 2"
)
)
[1]=> array(
[0]=> array (
["ID"]=> 135
["post_date"]=> "2014-04-28 06:37:37"
["post_title"]=> "Title 3"
)
[1]=> array (
["ID"]=> 148
["post_date"]=> "2014-04-25 07:27:37"
["post_title"]=> "Title 4"
)
)
[2]=> array(
[0]=> array (
["ID"]=> 135
["post_date"]=> "2014-04-24 06:37:37"
["post_title"]=> "Title 5"
)
[1]=> array (
["ID"]=> 148
["post_date"]=> "2014-04-25 09:21:37"
["post_title"]=> "Title 6"
)
)
)
Now I need to sort this using post_date
and show them in DESC
order. This is in PHP.
I am not sure how to sort this. Can anybody please help me with this sorting?
This is how I want to show the output:
title 2
Title 1
Title 3
Title 6
Title 4
Title 5
Okay, I have solved it by flattening the array and then applying usort
. This is how I have done it:
$query_results; //this is my main array
$flatten_array =array();
foreach ($query_results as $data) {
foreach($data as $flatten_data) {
$flatten_array[] = $flatten_data;
}
}
function cpt_array_sort($a, $b) {
return strtotime($b->post_date) - strtotime($a->post_date);
}
usort($flatten_array, 'cpt_array_sort');