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

I have been able to fetch the first-step arrays by looping through in blade file, however, I am having a little challenge in extracting the nested "capital" array in the above. My blade file code is below...

@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

So far I have been getting errors like "Undefined array key", "Cannot call string on a string", etc. I would be very glad if someone can kindly help me out with this. Also, I have done the necessary Google search but all was to no avail. Thanks.

Data source: RapidApi Countries API Documentation

cd4success
  • 77
  • 1
  • 9

2 Answers2

0

Try like this $countryCapital['name']

0

$country['capital'] is array, iterate on that like that:

@foreach ($country['capital'] as $countryCapital)
    <p>Capital: <strong>{{$countryCapital['name']}}</strong></p>
@endforeach
Enver Arslan
  • 709
  • 3
  • 13