0

OK, let's say when a user selects a country, they are also added with a "federation". These federations are pretty much region-centric.

Let's say I have something like this:

function getFedration($country_iso) {
 // 6 federations
 // afc = asian nations
 // caf = african nations
 // cocacaf = north & central america and Caribbean nations
 // conmebol = south america
 // ofc = Oceanian nations
 // uefa = european nations

$afc = array("Japan", "China", "South Korea");
$caf = array("Cameroon", "Chad", "Ivory Coast");
$concacaf = array("United States" , "Canada", "Mexico");
$conmebol = array("Argetina", "Brazil", "Chile");
$ofc = array("Fiji", "New Zealand", "Samoa");
$uefa = array("Spain", "England", "Montenegro");

/*
PSEUDO-code

If $country_iso is in either of six arrays... mark that as the federation...

*/

return $federation;

}

I know, it says a country's name but when it comes down to it, it will be country's iso like JP instead of Japan, CN instead of China, et cetera.

So, I was wondering, is this a feasible thing or is there a better way you'd think?

4
  • 1
    what is the output you want? can you please clarify?
    – Netorica
    Oct 4, 2012 at 5:21
  • Is this a good way to handle what I'm doing or is there an easier way? I want, when a user selects a country to look for country in one of the six arrays and return...
    – test
    Oct 4, 2012 at 5:22
  • 2
    @weka: to confirm by giving an example, if $country_iso is say "England" (but preferably "EN"), do you want "federation" to be "uefa"? Oct 4, 2012 at 5:23
  • Yes, that is exactly right, bob-the-destroyer.
    – test
    Oct 4, 2012 at 5:33

2 Answers 2

1

How about putting all federations into an array, in order to loop through it? Makes things easier, like so:

function countryToFederation($country_iso) {
    $federations = array(
        "afc" => array("Japan", "China", "South Korea"),
        "caf" => array("Cameroon", "Chad", "Ivory Coast"),
        "concacaf" => array("United States" , "Canada", "Mexico"),
        "conmebol" => array("Argetina", "Brazil", "Chile"),
        "ofc" => array("Fiji", "New Zealand", "Samoa"),
        "uefa" => array("Spain", "England", "Montenegro"),
    );


    foreach($federations as $federation) {
        if(in_array($country_iso, $federation)) {
            return $federation;
        }
    }
}
1
  • I get ` Parse error: syntax error, unexpected '=', expecting ')'` starting on the first line where it says "afc"
    – test
    Oct 4, 2012 at 14:53
1

If a federation can only belong to one country, I would create one array instead:

$countryToFederationMap = array(
    'Japan' => 'AFC',
    'China' => 'AFC',
    'Cameroon' => 'CAF',
    // ...
);

Then the federation is simply:

return $countryToFederationMap[$country];

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.