Search code examples
phpforeachphpstorm

The 'queries' variable is already used as an 'array expression'


I am importing countries from an API like so

public function importAPI()
{

    $url = 'https://restcountries.eu/rest/v2/';
    $json = file_get_contents($url);

    $countries = [];

    $queries = json_decode($json, true);

    foreach ($queries as $item['name']){
        foreach ($queries as $country){
             $countries[] = $country['name'];
        }
    }
    return $countries;
}

I am getting the error mentioned in the title

The 'queries' variable is already used as an 'array expression'

And I don't quite understand it, since the result that I return is correct.

Is this something that I need to worry about or have to change?


Solution

  • Since your end goal appears to be to obtain an array of country names, you can just use the following:

    public function importAPI()
    {
        $url  = 'https://restcountries.eu/rest/v2/';
        $json = json_decode( file_get_contents($url) );
    
        $countries = [ ];
    
        foreach( $json as $country )
        {
            $countries[] = $country->name;
        }
    
        return $countries;
    }