在内部值php上排序多维数组

时间:2014-08-19 16:08:25

标签: php arrays multidimensional-array

假设我有以下数组,如何在sort_by上对其进行排序?

Array
(
    [10] => Array
        (
            [Masthead_slide] => Array
                (
                    [id] => 1456464564
                    [sort_by] => 1
                )

        )

    [6] => Array
        (
            [Masthead_slide] => Array
                (
                    [id] => 645454
                    [sort_by] => 10
                )

        )

    [7] => Array
        (
            [Masthead_slide] => Array
                (
                    [id] => 4547
                    [sort_by] => 5
                )

        )
)

1 个答案:

答案 0 :(得分:1)

试试这个

function sortByOrder($a, $b) {
    return $a['sort_by'] - $b['sort_by'];
}
usort($arr, 'sortByOrder');