如何根据包含 UNIX 时间戳值的键按升序和降序对关联数组进行排序?
How to sort an associative array in ascending & descending order based on a key that contains the UNIX time stamp value?
我有一个名为 $things
的关联数组,如下所示:
注意:为了便于理解,我仅将关联数组 $things
中的几个元素放在下面。实际数组非常庞大,包含很多这样的内部数组。
下面是语句print_r($things);
的输出
Array
(
[0] => Array
(
[is_liked] =>
[comment_id] => 1654
[parent_id] => 0
[type_id] => image
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438072030
)
[1] => Array
(
[is_liked] =>
[comment_id] => 1657
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438080858
)
[2] => Array
(
[is_liked] =>
[comment_id] => 1658
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438082006
)
[3] => Array
(
[is_liked] =>
[comment_id] => 1659
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083448
)
[4] => Array
(
[is_liked] =>
[comment_id] => 1660
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083459
)
[5] => Array
(
[is_liked] =>
[comment_id] => 1661
[parent_id] => 0
[type_id] => text
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083467
)
)
有人可以帮助我根据每个内部数组中存在的时间戳值对上述关联数组进行升序和降序排序吗?
数组排序机制必须高效可靠,因为数组可能包含大量要排序的数据。
提前致谢。
尝试使用 usort
作为
升序
usort($your_array,function($a,$b){
return $a['time_stamp'] - $b['time_stamp'];
});
降序
usort($your_array,function($a,$b){
return $b['time_stamp'] - $a['time_stamp'];
});
注意:未测试
您可以为此使用 array_multisort。
我已经使用 usort
实现了相同的功能
usort($newArray, array($things, 'sort'));
我有一个名为 $things
的关联数组,如下所示:
注意:为了便于理解,我仅将关联数组 $things
中的几个元素放在下面。实际数组非常庞大,包含很多这样的内部数组。
下面是语句print_r($things);
Array
(
[0] => Array
(
[is_liked] =>
[comment_id] => 1654
[parent_id] => 0
[type_id] => image
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438072030
)
[1] => Array
(
[is_liked] =>
[comment_id] => 1657
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438080858
)
[2] => Array
(
[is_liked] =>
[comment_id] => 1658
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438082006
)
[3] => Array
(
[is_liked] =>
[comment_id] => 1659
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083448
)
[4] => Array
(
[is_liked] =>
[comment_id] => 1660
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083459
)
[5] => Array
(
[is_liked] =>
[comment_id] => 1661
[parent_id] => 0
[type_id] => text
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083467
)
)
有人可以帮助我根据每个内部数组中存在的时间戳值对上述关联数组进行升序和降序排序吗?
数组排序机制必须高效可靠,因为数组可能包含大量要排序的数据。
提前致谢。
尝试使用 usort
作为
升序
usort($your_array,function($a,$b){
return $a['time_stamp'] - $b['time_stamp'];
});
降序
usort($your_array,function($a,$b){
return $b['time_stamp'] - $a['time_stamp'];
});
注意:未测试
您可以为此使用 array_multisort。
我已经使用 usort
实现了相同的功能usort($newArray, array($things, 'sort'));