为什么内部数组元素在下面的场景中没有按时间戳值的降序排序?

时间:2015-07-29 08:13:31

标签: php arrays sorting associative-array key-value

我的标题为$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;

我不明白为什么它没有按时间戳值的降序排序?

请有人帮助我。

提前致谢。

2 个答案:

答案 0 :(得分:1)

因为数组列comments的结构可能不是$things['comments']' but $ thing [$ i] [' comments']。或者更容易使用foreach循环,例如

foreach($things as &$th) 
    usort($th['comments'], 
           function($a,$b) { return $b['time_stamp'] - $a['time_stamp']; }); 

答案 1 :(得分:1)

我认为您需要首先在数组中检查它是否包含uint8_t my_enroll_function(uint16_t test1, uint16_t test2) { ... } void my_change_function(uint64_t post1, uint8_t post2) { ... } int main(void) { ClientAlloc(my_enroll_function, my_change_function); } 密钥,然后需要使用time_stamp作为

usort