用字符串爆炸,在数组中搜索并返回其值

时间:2018-08-09 11:19:39

标签: php explode

我正在尝试将Country slug转换为短名称。所以我创建了这个函数:

from collections import Counter
def is_unique(l):
    return list(Counter(l).values()).count(1)==len(l)
print(is_unique(a))

必须打印 UAE ,但不起作用。

1 个答案:

答案 0 :(得分:0)

array_search将找到相等的文本。对于您的情况,我认为我们应该使用strpos

function strpos_array($countryArray, $countrySlug) {
    if (is_array($countryArray)) {
        foreach ($countryArray as $country) {
            if (($pos= strpos($country, $countrySlug)) !== false) {
                return str_replace($countrySlug.': ', '', $country);
            };
        }
    }
    return false;
}

function convertCountry($countrySlug) {
    $countries = "Andorra: AR|United-Arab-Emirates: UAE|Afghanistan: AFG|Antigua-And-Barbuda: AAB|Anguilla: ANG|Albania: ALB|Armenia: ARM";

    $countryArray = explode('|', $countries);

    return strpos_array($countryArray, $countrySlug);
}
echo convertCountry('United-Arab-Emirates');
//This will print UAE