在php中,我必须打印一些看起来像“”的国会大厦是“”的东西。PHP键和值capitols和国家
我面临的挑战是,当我尝试使用while循环对数据进行排序时,我没有收到结果。我明白,在这个例子中,我的国家是关键,大写字母就是价值。所以我用ksort以及asort来让他们都返回。我得到我的回声声明,但没有从我的$索引或$欧盟声明。试图找出我出错的地方以及我可以做得更好的建议?思考?谢谢
P.S我想我可能需要使用一个foreach循环,但老实说我可以使用数组中的while循环返回数据。
<?
$EU = array("Italy"=>"Rome", "Luxembourg"=>"Luxembourg",
"Belgium"=> "Brussels", "Denmark"=>"Copenhagen",
"Finland"=>"Helsinki", "France" => "Paris",
"Slovakia"=>"Bratislava", "Slovenia"=>"Ljubljana",
"Germany" => "Berlin", "Greece" => "Athens",
"Ireland"=>"Dublin", "Netherlands"=>"Amsterdam",
"Portugal"=>"Lisbon", "Spain"=>"Madrid",
"Sweden"=>"Stockholm", "United Kingdom"=>"London",
"Cyprus"=>"Nicosia", "Lithuania"=>"Vilnius",
"Czech Republic"=>"Prague", "Estonia"=>"Tallin",
"Hungary"=>"Budapest", "Latvia"=>"Riga", "Malta"=>"Valetta",
"Austria" => "Vienna", "Poland"=>"Warsaw");
$index = 0;
ksort($EU);
asort($index);
While ($index <11)
{
echo " The Capitol of {$EU[$index]} is. <br/>";
$index++;
}
?>
如果你看看PHP给你的警告,你将会至少修复排序问题。我想你可能需要查看ksort,asort的API文档。看看这里:http://sandbox.onlinephpfunctions.com/ – jdv
_“所以我使用ksort以及asort来让它们都返回_ _ - 这两个_sort_数组,但它们并没有改变任何关于它的结构。你真的在寻找函数array_keys和/或array_values也许......? – CBroe
'$ index = 0; asort($ index);' - 这没有任何意义,你不能“排序”一个整数。 – CBroe