0

I have a multidimensional countries array containing :

  • code -> Country Code
  • name - > Country Name
  • flag -> CSS sprite flag class

The needle is a countryCode and haystack the $countriesArray. How would you search for a string(countryCode) and return the corresponding name and flag. Is this array badly constructed?.

Array

$CountriesArray = array(array( code => "bj", 
                               name => "Benin",
                               flag => "flag flag-bj"
                            ),

                        array( code => "bw", 
                               name => "Botswana",
                               flag => "flag flag-bw"
                            ),

                        array( code => "cg", 
                               name => "Congo",
                               flag => "flag flag-cg"
                             )
  ect....

  );

ANSWER -Array format updated where the key is the countryCode

$CountriesArray = array(  array( bj => array( name=> "Benin", flag => "flag flag-bj" )), 
                          array( bw => array( name=> "Botswana", flag => "flag flag-bw" )),
                          array( cg => array( name=> "Congo", flag => "flag flag-cg" ))
                   );

Search array where $n = needle

function multiDimenArraySearch($n,$h){
foreach($h as $key => $value){
if($key == $n) return $value;
  }
}

$n = "bj";
$search = multiDimenArraySearch($n,$CountriesArray);

$name = $search[$n]["name"]; //outputs name
$flag = $search[$n]["flag"]; // outputs flag class
Awena
  • 1,002
  • 5
  • 20
  • 43
  • A meaningful key would make it better (for instance... `countryCode`), as it stands just looping in a `foreach` & checking for if `$currentvalue['code'] == $countryCode` is about as good as it gets, no need to make it more elaborate. – Wrikken Mar 12 '14 at 20:24
  • you mean like $CountriesArray = array(bj=>array(name=>"Benin",flag=>"flag flag-bj"),array(bw=>array(name=>"Botswana" ect... – Awena Mar 12 '14 at 20:30
  • Yep, that would make it work a lot more efficient. – Wrikken Mar 12 '14 at 20:31

2 Answers2

1

Same question over here, basically you just have to loop through all the different arrays. In the example below $return_val will be the array of the country.

$search_term = 'cg';
foreach($countriesArray as $c_info){
   if($c_info['code']==$search_term){
      $return_val = $c_info;
   }
}
Community
  • 1
  • 1
nickbwatson
  • 153
  • 7
0
for($i = 0; $i <= count($countriesArray); $i++){
    if($countriesArray[$i]["code"] == "cg"){
        return $countriesArray[$i]["name"];
    }
}

this should work

foreach version:

foreach($countriesArray as $country){
    if($country["code"] == "cg"){
        ...
    }
}

Example function:

function multiDimenArraySearch($n,$h){
    foreach($h as $arr){
        if($arr['code'] == $n){
            return $arr;
        }
    }
}

$search = multiDimenArraySearch("cg",$countriesArray);
$name = $search["name"];

For search value is array key:

foreach($h as $key => $value){
    if($key == "cg") return $value;
}
  • Thanks Nasaorc, your foreach version works fine. If anyone is wondering the same, please read above Wrikken recommendation. – Awena Mar 12 '14 at 20:35
  • Updated answer for search array key –  Mar 12 '14 at 20:38
  • Wait what? If we have the key we just `return $array[$key];`, why would we ever need a `for` loop there? – Wrikken Mar 13 '14 at 09:01