исправить для этого массива multidimosenal foreach l oop выберите, я делаю функцию, но не работает, может кто-нибудь помочь мне решить эту проблему? - PullRequest
0 голосов
/ 28 апреля 2020

я пытаюсь сделать foreach l oop для этой страны и сделать его в виде тега html, но не работает

$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') ;>//multidimesional array
//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('IN','my-country', 'my-country', 'form-control');

 i'ts not work!!!!!!
------------------------------------------------------------------------

1 Ответ

1 голос
/ 28 апреля 2020

В вашем коде есть ошибка с определением $ 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');

А вот полный исходный код.

<?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');
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...