通过两个变量对多维数组进行排序 - PHP

时间:2013-04-04 14:56:30

标签: php arrays sorting

我需要通过键值对数组php进行排序。我的阵列:

Array
(
    [1430039342393636453] => Array
        (
            [0] => Array
                (
                    [thrid] => 1430039342393636453
                    [uid] => 19748
                    [flag] => 1
                    [timestamp] => 1363791789
                    [date] => Mar 20
                    [content_preview] => 
                    [content] => 

                )

        )
[1430750471744336569] => Array
        (
            [0] => Array
                (
                    [thrid] => 1430750471744336569
                    [uid] => 19870
                    [flag] => 1
                    [timestamp] => 1364469959
                    [date] => Mar 28
                    [content_preview] =>
                    [content] =>
                )

            [1] => Array
                (
                    [thrid] => 1430750471744336569
                    [uid] => 19874
                    [flag] => 1
                    [timestamp] => 1364472417
                    [date] => Mar 28
                    [content_preview] => 
                    [content] => 
                )
)

我需要按timestamp主数组和子数组排序。 有什么建议吗?

2 个答案:

答案 0 :(得分:1)

使用asort对关联数组进行排序。

相关的S.O.交:

Sorting an associative array in PHP

答案 1 :(得分:0)

尝试类似:

private function sort($sort) {

    foreach($sort as &$arr) { //use a reference because usort uses references to manipulate your array.
                            //if you don't pass by reference you'll never see the sort
        usort($arr, function($a, $b) { return $a['timestamp'] - $b['timestamp'];
    }

    usort($sort, function($a, $b) { return key($a) - key($b); }

    return $sort;

}