1 回答
TA贡献1869条经验 获得超4个赞
根据您的示例,您实际上是在尝试areas对子数组进行排序,而不是对整个父数组进行排序。因此,您需要依次循环每个子数组并分别对它们进行排序。
$array = [
'chicago' => [
'community_name' => 'Chicago, IL',
'areas' => [
[
'name' => ['HELLO WORLD.']
],
[
'name' => ['Hello'],
],
[
'name' => ['Administration.'],
],
],
],
'chicago-and-surrounding-areas' => [
'community_name' => 'Chicago (and surrounding areas), IL',
'areas' => [
[
'name' => ['Carry.']
],
[
'name' => ['Bacteria.'],
],
],
],
'cambridge-chicago' => [
'community_name' => 'Cambridge (Chicago), IL',
'areas' => [
[
'name' => ['Responsibility.']
],
[
'name' => ['Bacteria'],
],
],
],
];
foreach ($array as &$locality) {
usort($locality['areas'], function ($a, $b) {
return $a['name'][0] <=> $b['name'][0];
});
}
var_dump($array);
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报