小编典典

按子值对php多维数组排序

php

我有这个数组

Array
(
[data] => Array
    (
        [0] => Array
            (

                [id] => 1293005125
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293006034
                [initial_timestamp] => 1293005125
                [user] => administrator
            )

        [1] => Array
            (

                [mid] => 1293001908
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293001908
                [initial_timestamp] => 1293001908
                [user] => administrator
            )

        [2] => Array
            (

                [mid] => 1293009999
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293009999
                [initial_timestamp] => 1293009999
                [user] => administrator
            )

        [3] => Array
            (

                [mid] => 1293006666
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293006666
                [initial_timestamp] => 1293006666
                [user] => administrator
            )

        [4] => Array
            (

                [mid] => 1293005125
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293006125
                [initial_timestamp] => 1293005125
                [user] => administrator2
            )


    )

现在,我想按此方法对数组进行排序[mid]

目前,我在一个foreach循环中对此进行排序 必须有更好的方法

编辑 我希望输出类似

[mid] key => array value

谢谢


阅读 217

收藏
2020-05-26

共1个答案

小编典典

您可以使用usort函数。

function cmp($a, $b) {
        return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");
2020-05-26