-1
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 [▶]

我已经能够通过在刀片文件中循环来获取第一步数组,但是,我在提取上面的嵌套“资本” 数组时遇到了一点挑战。我的刀片文件代码如下...

@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 文档

4

2 回答 2

0

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

于 2022-01-29T13:16:02.523 回答
0

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

@foreach ($country['capital'] as $countryCapital)
    <p>Capital: <strong>{{$countryCapital['name']}}</strong></p>
@endforeach
于 2022-01-29T15:22:45.230 回答