よろしくお願いします。次の$events配列があり、events配列内の' cat '配列をsortの値で並べ替えたいと思います。
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']
}
}