按键值排序数组

时间:2014-11-25 05:12:35

标签: php arrays sorting

我有以下数组

Array ( [0] => Array ( [lastUpload] => [lastPost] => Sun Nov 23 2014 16:54:13 [username] => blt152561 [email] => user1@mail.com [joined] => Sun Nov 23 2014 16:49:09 [prodCount] => 2 [mediaCount] => 0 [userId] => 77292aaf-e540-465c-9319-8dd74c8d7f12 ) [1] => Array ( [lastUpload] => [lastPost] => [username] => mvbg40 [email] => user2@mail.com [joined] => Sun Nov 23 2014 9:31:43 [prodCount] => 0 [mediaCount] => 0 [userId] => a2998c68-1c9a-49b9-8c78-ced3cffaa109 ) [2] => Array ( [lastUpload] => Sun Nov 23 2014 7:54:03 [lastPost] => Sun Nov 23 2014 7:53:53 [username] => flexingladies [email] => user5@mail.com [joined] => Sun Nov 23 2014 7:47:44 [prodCount] => 1 [mediaCount] => 1 [userId] => 3b4b6aad-de95-428c-95ed-cb4fe963f322 ) [3] => Array ( [lastUpload] => [lastPost] => Sat Nov 22 2014 8:05:30 [username] => dewey [email] => user3@mail.com [joined] => Sat Nov 22 2014 8:02:01 [prodCount] => 1 [mediaCount] => 0 [userId] => 08220286-6a24-49ff-929b-ccb4e0e10c3e ) [4] => Array ( [lastUpload] => [lastPost] => Thu Nov 20 2014 18:43:56 [username] => mhirsen99 [email] => user4@mail.com [joined] => Thu Nov 20 2014 18:43:09 [prodCount] => 1 [mediaCount] => 0 [userId] => ddea4f04-b035-4e8d-a777-cb4a2813b705 ) )

我想按lastPost中的值排序。

我不知道该怎么做。有人能指出我正确的方向吗?我尝试了ksort,但它只返回1而不是排序数组。

3 个答案:

答案 0 :(得分:0)

尝试使用此

asort($array);

根据值

按升序对数组进行排序

arsort($array) ;

根据值

按降序对数组进行排序

答案 1 :(得分:0)

您可以使用usort按键进行自定义排序

usort($array, function($a, $b) { return $a['lastPost'] - $b['lastPost']; });

答案 2 :(得分:0)

你必须使用foreach来完成这项工作:

foreach ($array as $key => $value)
  $ordered[$value['lastPost']] = $value;

ksort($ordered, SORT_ASC);