Carry values outside the if?

I have this if:

foreach ($new_array_adm2 as $key => $val) {

if ($val['feature code'] === 'ADM3' ) 
       {
           
           $test = "".$val['country code']." ".$val['admin1 code']." ".$val['admin2 code']."";  
           
       }

echo $test;

}

Why $test doesn’t work here?

Array:

Array
(
    [0] => Array
        (
            [geonameid] => 6358177
            [name] => España
            [asciiname] => Alajar
            [alternatenames] => 
            [latitude] => 37.8394400
            [longitude] => -6.6507900
            [feature class] => A
            [feature code] => ADM3
            [country code] => ES
            [cc2] => 
            [admin1 code] => 51
            [admin2 code] => H
            [admin3 code] => 21001
            [admin4 code] => 
            [population] => 46505963
            [elevation] => 0
            [gtopo30] => 350
            [timezone] => Europe/Madrid
            [modification date] => 2015-10-15
            [iso_alpha2] => ES
            [iso_alpha3] => ESP
            [iso_numeric] => 724
            [fips_code] => SP
            [capital] => Madrid
            [areainsqkm] => 504782
            [continent] => EU
            [tld] => .es
            [currency] => EUR
            [currencyName] => Euro
            [Phone] => 34
            [postalCodeFormat] => #####
            [postalCodeRegex] => ^(d{5})$
            [geonameId] => 0
            [languages] => 2510769
            [neighbours] => AD,PT,GI,FR,MA
            [equivalentFipsCode] => 
        )

    [1] => Array
        (
            [geonameid] => 2521964
            [name] => España
            [asciiname] => Aljaraque
            [alternatenames] => Aljaraque
            [latitude] => 37.2698900
            [longitude] => -7.0231300
            [feature class] => P
            [feature code] => PPLA3
            [country code] => ES
            [cc2] => 
            [admin1 code] => 51
            [admin2 code] => H
            [admin3 code] => 21002
            [admin4 code] => 
            [population] => 46505963
            [elevation] => 0
            [gtopo30] => 30
            [timezone] => Europe/Madrid
            [modification date] => 2012-03-04
            [iso_alpha2] => ES
            [iso_alpha3] => ESP
            [iso_numeric] => 724
            [fips_code] => SP
            [capital] => Madrid
            [areainsqkm] => 504782
            [continent] => EU
            [tld] => .es
            [currency] => EUR
            [currencyName] => Euro
            [Phone] => 34
            [postalCodeFormat] => #####
            [postalCodeRegex] => ^(d{5})$
            [geonameId] => 0
            [languages] => 2510769
            [neighbours] => AD,PT,GI,FR,MA
            [equivalentFipsCode] => 
        )

    [2] => Array
        (
            [geonameid] => 6358179
            [name] => España
            [asciiname] => Almendro, El
            [alternatenames] => 
            [latitude] => 37.5743800
            [longitude] => -7.3761300
            [feature class] => A
            [feature code] => ADM3
            [country code] => ES
            [cc2] => 
            [admin1 code] => 51
            [admin2 code] => H
            [admin3 code] => 21003
            [admin4 code] => 
            [population] => 46505963
            [elevation] => 0
            [gtopo30] => 172
            [timezone] => Europe/Madrid
            [modification date] => 2015-10-15
            [iso_alpha2] => ES
            [iso_alpha3] => ESP
            [iso_numeric] => 724
            [fips_code] => SP
            [capital] => Madrid
            [areainsqkm] => 504782
            [continent] => EU
            [tld] => .es
            [currency] => EUR
            [currencyName] => Euro
            [Phone] => 34
            [postalCodeFormat] => #####
            [postalCodeRegex] => ^(d{5})$
            [geonameId] => 0
            [languages] => 2510769
            [neighbours] => AD,PT,GI,FR,MA
            [equivalentFipsCode] => 
        )

    

   )

My guess is you’re not going the level into the array that you think you are.

Try this and see if it shows what you think it should be.

foreach ($new_array_adm2 as $key => $val) {
  echo "key: " . $key . " val: " . $val . "<br>";
}

So what do you expect $test to be if the feature code is not ADM3? At the very least you need to set it to a known value within your foreach loop:

foreach ($new_array_adm2 as $key => $val) {
    $test = null;
    if ($val['feature code'] === 'ADM3' )  {
        $test = sprintf("%s %d %s", 
            $val['country code'],
            $val['admin1 code'] ,
            $val['admin2 code']);  
    }
    echo $test . "\n";
}

By the way, while spaces within keys are allowed they are a bit unusual. Might want to use country_code instead of counter code if possible.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.