Get administrative_area_level_1 from JSON based on country code?

[address_components] => Array
        (
            [0] => Array
                (
                    [long_name] => Guayaquil
                    [short_name] => Guayaquil
                    [types] => Array
                        (
                            [0] => locality
                            [1] => political
                        )

                )

            [1] => Array
                (
                    [long_name] => Guayas
                    [short_name] => Guayas
                    [types] => Array
                        (
                            [0] => administrative_area_level_1
                            [1] => political
                        )

                )

            [2] => Array
                (
                    [long_name] => Ecuador
                    [short_name] => EC
                    [types] => Array
                        (
                            [0] => country
                            [1] => political
                        )

                )

        )

How can I extract administrative_area_level_1 value based on the country code like EC

if (in_array("country", $json_address["types"])) {
            if($json_address["short_name"] == "EC")
			{
            if (in_array("administrative_area_level_1", $json_address["types"])) {
            $adm1_json[] = $json_address["long_name"];
        }
			}
        }

In other words if country is EC extract administrative_area_level_1

your code works for arrays like this:

$json_address = [
    'short_name' => 'EC',
    'long_name' => 'Ecuador',
    'types' => [
        'country',
        'administrative_area_level_1'
    ]
];
...
print_r($adm1_json); // [0] => Ecuador

you wrote this:

if types contains country, (and) if short_name is EC, (and) if types contains administrative_area_level_1

OK I am on the rignt track now thanks.

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