为什么在以下情况下内部数组元素没有按时间戳值的降序排序?
Why the inner array elements are not getting sorted in descending order of time stamp value in following scenario?
我有一个名为 $things
的关联数组,如下所示:
Array
(
[0] => Array
(
[feed_id] => 4930
[app_id] => 0
[privacy] => 0
[privacy_comment] => 0
[type_id] => poll
[likes] => Array
(
)
[marks] => Array
(
)
[comments] => Array
(
[0] => Array
(
[text] =>
)
[1] => Array
(
[text] =>
)
)
[server_time] => 1438157330
[user_group_name] =>
)
[1] => Array
(
[feed_id] => 4914
[app_id] => 0
[privacy] => 0
[privacy_comment] => 0
[type_id] => poll
[likes] => Array
(
)
[comments] => Array
(
[0] => 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
)
[1] => Array
(
[is_liked] =>
[comment_id] => 1661
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083467
)
)
)
[2] => Array
(
[feed_id] => 4926
[app_id] => 0
[privacy] => 0
[privacy_comment] => 0
[type_id] => poll
[likes] => Array
(
)
[marks] => Array
(
)
[comments] => Array
(
[0] => Array
(
[text] =>
)
[1] => Array
(
[text] =>
)
)
)
[comments] =>
)
我想在不保留键的情况下按时间戳值的降序对内部数组 ['comments']
进行排序,因此我为其编写了以下代码。
usort($things['comments'],function($a,$b) {
return $b['time_stamp'] - $a['time_stamp'];
});
print_r($things); die;
我不明白为什么它没有按时间戳值的降序排列?
请有人帮助我。
提前致谢。
因为数组列的结构 comments
可能无法像 $things['comments']' but
$things[$i]['comments'] 那样实现。或者更容易使用 foreach 循环,例如 so
foreach($things as &$th)
usort($th['comments'],
function($a,$b) { return $b['time_stamp'] - $a['time_stamp']; });
我认为您需要先在数组中检查它是否包含 time_stamp
键,然后需要使用 usort
as
foreach($things as &$th) {
if(array_key_exists('time_stamp',$th['comments'])){
usort($th['comments'],
function($a,$b) { return $b['time_stamp'] - $a['time_stamp']; });
}
}
我有一个名为 $things
的关联数组,如下所示:
Array
(
[0] => Array
(
[feed_id] => 4930
[app_id] => 0
[privacy] => 0
[privacy_comment] => 0
[type_id] => poll
[likes] => Array
(
)
[marks] => Array
(
)
[comments] => Array
(
[0] => Array
(
[text] =>
)
[1] => Array
(
[text] =>
)
)
[server_time] => 1438157330
[user_group_name] =>
)
[1] => Array
(
[feed_id] => 4914
[app_id] => 0
[privacy] => 0
[privacy_comment] => 0
[type_id] => poll
[likes] => Array
(
)
[comments] => Array
(
[0] => 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
)
[1] => Array
(
[is_liked] =>
[comment_id] => 1661
[parent_id] => 0
[type_id] => poll
[item_id] => 141
[user_id] => 901
[owner_user_id] => 901
[time_stamp] => 1438083467
)
)
)
[2] => Array
(
[feed_id] => 4926
[app_id] => 0
[privacy] => 0
[privacy_comment] => 0
[type_id] => poll
[likes] => Array
(
)
[marks] => Array
(
)
[comments] => Array
(
[0] => Array
(
[text] =>
)
[1] => Array
(
[text] =>
)
)
)
[comments] =>
)
我想在不保留键的情况下按时间戳值的降序对内部数组 ['comments']
进行排序,因此我为其编写了以下代码。
usort($things['comments'],function($a,$b) {
return $b['time_stamp'] - $a['time_stamp'];
});
print_r($things); die;
我不明白为什么它没有按时间戳值的降序排列?
请有人帮助我。
提前致谢。
因为数组列的结构 comments
可能无法像 $things['comments']' but
$things[$i]['comments'] 那样实现。或者更容易使用 foreach 循环,例如 so
foreach($things as &$th)
usort($th['comments'],
function($a,$b) { return $b['time_stamp'] - $a['time_stamp']; });
我认为您需要先在数组中检查它是否包含 time_stamp
键,然后需要使用 usort
as
foreach($things as &$th) {
if(array_key_exists('time_stamp',$th['comments'])){
usort($th['comments'],
function($a,$b) { return $b['time_stamp'] - $a['time_stamp']; });
}
}