This question already has an answer here:
这个问题在这里已有答案:
- How can I sort arrays and data in PHP? 8 answers
- 如何在PHP中对数组和数据进行排序? 8个答案
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');