0

好的,假设当用户选择一个国家时,他们也被添加了一个“联盟”。这些联合会几乎以地区为中心。

假设我有这样的事情:

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;

}

我知道,它说的是一个国家的名字,但归根结底,它会是国家的 iso ,而JP不是Japan,CN而不是China, 等等。

所以,我想知道,这是一个可行的事情还是你认为有更好的方法?

4

2 回答 2

1

如果一个联邦只能属于一个国家,我会创建一个数组:

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

那么联邦很简单:

return $countryToFederationMap[$country];
于 2012-10-04T05:24:27.370 回答
1

将所有联邦放入一个数组中,以便循环遍历它怎么样?让事情变得更容易,就像这样:

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;
        }
    }
}
于 2012-10-04T05:56:26.067 回答