Php 修复这个多维数组foreach循环select,我做函数但不工作,有人能帮我解决这个问题吗?
我正在尝试为此国家/地区创建foreach循环,并将其作为select html标记,但不起作用Php 修复这个多维数组foreach循环select,我做函数但不工作,有人能帮我解决这个问题吗?,php,Php,我正在尝试为此国家/地区创建foreach循环,并将其作为select html标记,但不起作用 $countryArray=array('AD'=>array('name'=>'andror','code'=>'376'), 'AE'=>数组('name'=>'UNITED ARAB EMIRATES','code'=>'971'), 'AF'=>数组('name'=>'AFGHANISTAN','code'=>'93'), 'AG'=>数组('name'=>'ANTIGUA AND BARB
$countryArray=array('AD'=>array('name'=>'andror','code'=>'376'),
'AE'=>数组('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>数组('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>数组('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>数组('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>array('name'=>'albana','code'=>'355');>//多维数组
//foreach select的函数
函数countrySelector($defaultCountry=“”,$id=“”,$name=“”,$classes
= ""){
全球$countryArray;
$output=“”;
foreach($countryArray作为$code=>$country){
$countryName=ucwords(strtolower($country[“name”]);
$output.=''.$code-
“$countryName.”(+“$country[“code”]。”);}
$output.='';返回$output;}
echo countrySelector('IN'、'my-country'、'my-country'、'form control');
我不工作!!!!!!
------------------------------------------------------------------------
您的代码与$countryArray的定义有误
数组定义末尾省略了“)”
最后应删除定义的“>”
看看这个
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>array('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>array('name'=>'ALBANIA','code'=>'355')
);
//function for foreach select
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes= ""){
global $countryArray;
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"]));
$output .= "<option value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>";
return $output;
}
echo countrySelector('AF','my-country', 'my-country', 'form-control');
$countryArray=array(
'AD'=>数组('name'=>'andror','code'=>'376'),
'AE'=>数组('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>数组('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>数组('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>数组('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>数组('name'=>'albana','code'=>'355')
);
//foreach select的函数
函数countrySelector($defaultCountry=“”、$id=“”、$name=“”、$classes=“”){
全球$countryArray;
$output=“”;
foreach($countryArray作为$code=>$country){
$countryName=ucwords(strtolower($country[“name”]);
$output.=''.$code.-“$countryName.”(+“$country[“code”]。”);
}
$output.=“”;
返回$output;
}
echo countrySelector('AF','my-country','my-country','form control');
这是完整的源代码
<?php
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>array('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>array('name'=>'ALBANIA','code'=>'355')
);
//function for foreach select
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes= ""){
global $countryArray;
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"]));
$output .= "<option value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>"; return $output;
}
echo countrySelector('AF','my-country', 'my-country', 'form-control');
?>
是的,我正在尝试复制所有代码,但没有再次工作:(您是否有任何错误消息?或者它没有显示任何内容?或者您的页面url
<?php
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>array('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>array('name'=>'ALBANIA','code'=>'355')
);
//function for foreach select
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes= ""){
global $countryArray;
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"]));
$output .= "<option value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>"; return $output;
}
echo countrySelector('AF','my-country', 'my-country', 'form-control');
?>