如何在Laravel中获取嵌套JSON数组的元素


array:249 [▼
0 => array:6 [▼
"name" => "Afghanistan"
"state_name" => "Islamic Emirate of Afghanistan"
"capital" => array:1 [▼
0 => array:5 [
0: {5 items
"name":"Kabul"
"notes":"official"
"latitude":"34.575503"
"longitude":"69.240073"
"population":"3140853"
}]
]
"iso_3166" => array:4 [▼
"alpha2" => "AF"
"alpha3" => "AFG"
"numeric" => "004"
"subdivision" => "ISO 3166-2:AF"
]
"un_geoscheme" => array:2 [▼
"region" => "Asia"
"subregion" => "Southern Asia"
]
"population" => array:3 [▼
"density_km" => "50.38"
"total" => "32890171"
"density_mi" => "130.48"
]

]

1 => array:6 [▶]
2 => array:6 [▶]

我已经能够通过在blade文件中循环来获取第一步数组,然而,在提取嵌套的"大写">数组。我的刀片文件代码如下。。。

@foreach ($countriesData as $country)
<div class="p-2">
<div class="flex items-center">
<div class="ml-4 text-lg leading-7 font-semibold"><p>Country: <strong>{{ $country['name'] }}</strong></p></div>
</div>
<div class="ml-12">
<div class="mt-2 dark:text-gray-400 text-sm">
<p>Full name: <strong>{{$country['state_name']}}</strong></p>
<p>Continent: <strong>{{$country['un_geoscheme']['region']}}</strong></p>
@foreach ($country as $countryCapital)
{{-- <p>Capital: <strong>{{$countryCapital['capital']['name']}}</strong></p>

@endforeach
{{--  Do I foreach again as I have done above, use a for loop or what must I do in this particular scenario? Thanks --}}
</div>
</div>
</div>
@endforeach

到目前为止,我已经收到了类似于"的错误;未定义的数组键">";无法对字符串"调用字符串等。如果有人能帮我解决这个问题,我将非常高兴。此外,我做了必要的谷歌搜索,但一切都无济于事。谢谢

数据源:RapidApi国家/地区API文档

像这样尝试$countryCapital['name']

$country['capital']是数组,像这样迭代:

@foreach ($country['capital'] as $countryCapital)
<p>Capital: <strong>{{$countryCapital['name']}}</strong></p>
@endforeach

相关内容

  • 没有找到相关文章

最新更新