Wes
Wes

Reputation: 856

How to reformat this array without knowing the keys in advance

I'm querying an API and getting a response back with various countries. Here is the relevant array I'm working with and what it prints out.

    print_r($apiResponse['response']['data'][0]['countries']);

prints this:

Array ( [US] => Array ( [id] => 840 [code] => US [name] => United States [regions] => Array ( ) ) [CA] => Array ( [id] => 124 [code] => CA [name] => Canada [regions] => Array ( ) ) )

I am looking to save an array of only the two character country codes from that data. The only thing is the key is unknown to me when I query it so I don't know how to access the [code] section of it to save it to my new array.

I want to end up being able to take whatever amount of countries the API sends back and save the two character codes in a format like this:

'country_codes' => array('US','CA','UK','AU')

Thanks for your help!

Upvotes: 0

Views: 44

Answers (1)

drosam
drosam

Reputation: 2896

Use the array_keys() function. Here you have the documentation.

Upvotes: 1

Related Questions