请您帮忙,我有以下$events数组,我想按 sort 的值对我的 events 数组中的 ' cat ' 数组进行排序。
Array ( [0] => Array (
[date] => 20130329
[title] => Run
[cats] => Array (
[0] => Array (
[name] => Cause 2
[slug] => cause-2
[sort] => 1) )
)
[1] => Array (
[date] => 20130131
[title] => Run2
[cats] => Array (
[0] => Array (
[name] => Abused Children
[slug] => abused-children
[sort] => 2 )
[1] => Array (
[name] => Animal Welfare
[slug] => animal-welfare
[sort] => 3 )
[2] => Array (
[name] => Education
[slug] => education
[sort] => 1 )
) )
当我在 [cat] 上执行 foreach 时,我希望它按排序顺序进行迭代,例如。
Event: Run2
Cat: Education
Cat: Abused Children
Cat: Animal Welfare
在我的代码中,我有这样的东西
usort($events, function($a, $b) {
return $a['cats']['sort'] - $b['cats']['sort']; });
foreach($events as $event) {
foreach($event['cats'] as $cat) {
echo $cat['name']
}
}