問題描述
需要有關排序要求的幫助 (Need assistance with a usort requirement)
I've taken a look at a few googles and actually found this stack too: How to sort a date array in PHP
I'ts on the same wave length but i'm not sure i get it clearly. I've read over the phpnet documentation too... As I said, i'm feeling shakey from it.
I have an array:
Array
(
[0] => Array
(
[created] => 2012-06-06 21:26:25
)
[1] => Array
(
[created] => 2012-06-06 21:23:45
)
)
And I basically need to sort this array so that they are in date order. How does this call back function work for usort? Any examples would be great!
參考解法
方法 1:
function MySort($a, $b)
{
if ($a['created'] == $b['created']) return 0;
return $a['created'] < $b['created'] ? -1 : 1;
}
then use...
usort($myarray, "MySort");
方法 2:
usort
just lets you sort using your own criteria. You can just simply do this:
usort($array, function($a, $b){
$a = strtotime($a['created']);
$b = strtotime($b['created']);
return $a-$b;
});
(by Jimmyt1988、craig1231、gen_Eric)