Dan*_*est 5 php arrays sorting datetime
我目前在PHP中有一个问题,我想按照创建日期对这些帖子进行排序,以便它们可以按降序显示.我一直在寻找一个PHP函数来做到这一点,但没有运气.
有一个简单的解决方案吗?任何想法将不胜感激:)
array
0 =>
array
'post_id' => string '1' (length=1)
'user_id' => string '3' (length=1)
'post' => string 'this is a post' (length=14)
'created' => string '2012-04-05 20:11:38' (length=19)
1 =>
array
'post_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'post' => string 'this is a post' (length=14)
'created' => string '2012-04-05 20:11:38' (length=19)
2 =>
array
'post_id' => string '3' (length=1)
'user_id' => string '5' (length=1)
'post' => string 'this is a post' (length=14)
'created' => string '2012-04-05 20:11:38' (length=19)
Run Code Online (Sandbox Code Playgroud)
试试这个:
<?php
$a=array(
0 =>
array(
'post_id' => '1',
'user_id' => '3',
'post' => 'this is a post',
'created' => '2012-04-05 20:11:40'
),
1 =>
array(
'post_id' => '2',
'user_id' => '2',
'post' => 'this is a post',
'created' => '2012-04-05 20:11:39'
),
2 =>
array(
'post_id' => '3',
'user_id' => '5',
'post' => 'this is a post',
'created' => '2012-04-05 20:11:38'
)
);
function cmp($a,$b){
return strtotime($a['created'])<strtotime($b['created'])?1:-1;
};
uasort($a,'cmp');
print_r($a);
?>
Run Code Online (Sandbox Code Playgroud)