1

I have this array:

    static $countryList = array(
       ["AF" => "Afghanistan"],
       ["AL" => "Albania"],
       ["DZ" => "Algeria"],
       //many more countries
      );

I want to do something like $countryList['DZ'] to get "Algeria"

why those damned sub arrays?

well, some countries must come twice

basically this...

    static $countryList = array(
       ["US" => "USA"],
       ["AL" => "Albania"],
       ["DZ" => "Algeria"],
       //...
       ["UB" => "Uganda"],
       ["US" => "USA"]
      );

it's used for a select list

1
  • @hanshenrik He explained why he did it, because he needs to allow duplicates. Commented Nov 3, 2017 at 20:51

1 Answer 1

1

Make another array that's an associative array:

$countryMap = [];
foreach ($countryList as $country) {
    foreach ($country as $short => $long) {
        $countryMap[$short] = $long;
    }
}

Then you can use $countryMap["DZ"]

Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping there might be a better solution that iterating through the array
Nope, there's nothing built in that searches sub-arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.