I’m trying to sort my array by the timestamp which is in 'sort' => $date
this line. I’ve tried doing it with array_multisort
, but can't make it work. At the moment it just prints the $data
array in the same order as normal. What am I doing wrong in array_multisort
?
My timesmap looks like this:
1397755920
Here is my code:
$data = array();
for ($x=0; $x<=count($obj)-1; $x++) {
$date = strtotime(str_replace('/', '-', $obj[$x]['time']));
$post_data = array(
'title' => $obj[$x]['title'],
'link' => $obj[$x]['link'],
'image' => $obj[$x]['image'],
'content' => $obj[$x]['content'],
'time' => $obj[$x]['time'],
'sort' => $date,
'name' => $obj[$x]['name']);
$data[] = $post_data;
}
array_multisort($data['sort'], SORT_DESC);
print_r($data);
USORT Example:
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
for ($x=0; $x<=count($data)-1; $x++) {
usort($data[$x]['sort'],"my_sort");
}
print_r($data);