我想在这个数组中找到国家iso代码:
$countryArray = array(
'AD' => array(
'country_name' => 'ANDORRA',
'dial_code' => '376'
),
'AE' => array(
'country_name' => 'UNITED ARAB EMIRATES',
'dial_code' => '971'
),
'AF' => array(
'country_name' => 'AFGHANISTAN',
'dial_code' => '93'
));
我的条件是:
foreach($countryArray as $row){
if($row->dial_code == '93'){
echo $row; //???
}
}
在echo语句中,我想在这个例子中显示AF。 但结果是错误的。
答案 0 :(得分:3)
foreach($countryArray as $key => $row){
if($row['dial_code'] == 93){
echo $key; //???
}
}
尝试这个,只需在foreach循环中使用$key
变量
答案 1 :(得分:2)
$key=>$value
对将当前键元素分配给$ key,将当前值分配给$ value。我们知道foreach语句适用于内部指针。
$countryArray = array(
'AD' => array(
'country_name' => 'ANDORRA',
'dial_code' => '376'
),
'AE' => array(
'country_name' => 'UNITED ARAB EMIRATES',
'dial_code' => '971'
),
'AF' => array(
'country_name' => 'AFGHANISTAN',
'dial_code' => '93'
));
foreach($countryArray as $key =>$value){
if($value['dial_code'] == 93){
echo $key;
}
}
答案 2 :(得分:1)
试试这个
foreach($countryArray as $k => $row){
if($row["dial_code"] == 93){
echo $k;
}
}
答案 3 :(得分:0)
我解决了它,在代码工作正常
$countryArray = array(
'AD' => array(
'country_name' => 'ANDORRA',
'dial_code' => '376'
),
'AE' => array(
'country_name' => 'UNITED ARAB EMIRATES',
'dial_code' => '971'
),
'AF' => array(
'country_name' => 'AFGHANISTAN',
'dial_code' => '93'
));
foreach ($countryArray as $keys=> $arraycountry){
foreach($arraycountry as $key => $value)
{
if($value == '93'){
echo $keys; //???
}
}
}