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

时间:2022-01-14 21:21:10

This question already has an answer here:

这个问题在这里已有答案:

Say I have the following array, how can I sort it on sort_by?

假设我有以下数组,如何在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 个解决方案

#1


1  

Try by this

试试吧

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

#1


1  

Try by this

试试吧

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