开发者

php sort array with three dimensions

开发者 https://www.devze.com 2023-03-20 04:51 出处:网络
I have a three dimensional array and I want to sort the arrays in the first dimension on a value in the third dimension.

I have a three dimensional array and I want to sort the arrays in the first dimension on a value in the third dimension. Like the following:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [data] => 7
                [field] => 1
                [type] => string
            )

        [1] => Array
            (
                [data] => 2011/07/13
                [field] => -2
                [sort] => 1310594015
            )

        [2] => Array
           开发者_开发知识库 (
                [data] => admin
                [field] => -1
                [sort] => admin
            )
    )

[1] => Array
    (
        [0] => Array
            (
                [data] => 6
                [field] => 1
                [type] => string
            )

        [1] => Array
            (
                [data] => 2011/07/14
                [field] => -2
                [sort] => 1310667194
            )

        [2] => Array
            (
                [data] => admin
                [field] => -1
                [sort] => admin
            )
    )
)

I would like to sort the arrays at the first level (i.e. there are only 2 of them) based on the value of the 'data' key, down in the third level. In this case, the two arrays at the first level should swap so the array with the value of 6 for 'data' comes before the one with 7. The array that contains the 'data' key will always be at position [0] of the second level.


usort($allArray,function($a,$b){
     return $a[0]['data']-$b[0]['data'];
})
0

精彩评论

暂无评论...
验证码 换一张
取 消