【发布时间】:2016-01-05 15:17:29
【问题描述】:
我有一个 foreach,我想比较两个数组中的一些字符串
变量 $count_land 的输出
Array
(
["US"] => 186
["DE"] => 44
["BR"] => 15
["FR"] => 3
["other"] => 7
)
其他数组:
$all_lands_array = ["CN", "DE", "US", "EU", "RU", "BR", "GB", "NL", "LU", "other", "IT"];
我需要的是 $all_lands_array 中的每个键都具有来自 $count_land 数组的相同字母并将它们保存在另一个变量中
这意味着我需要获取美国、德国、巴西、法国和其他并将它们保存在一个新变量中
我希望你明白我需要什么:)
这是我的循环,但它没有找到任何匹配项.. 但是为什么呢?
foreach($all_lands_array as $lands){
if($lands == $count_land)
return "match";
else
return "no match!";
}
【问题讨论】:
-
您正在将字符串与数组进行比较
-
只需在键中使用
in_array()进行搜索,您将获得:array_keys($count_land)