我真的很努力使这项工作,我看过指南,但我似乎看不到我和他们的之间的区别,除了数组的布局方式。
<?php
$country = array('England' => "London", 'Scotland' => "Edinburgh", 'France' => "Paris");
foreach ($capitals as $country=>$capital) {
echo "The capital of $country is $capital";
}
?>
我想要它做的只是说这个国家和它的首都。
错误代码 --
Notice: Undefined variable: capitals in C:'xampp'htdocs'foreach.php on line 4
Warning: Invalid argument supplied for foreach() in C:'xampp'htdocs'foreach.php on line 4
提前谢谢。
将代码更改为以下内容:
(我将数组名称从country
更改为capitals
(
<?php
$capitals = array('England' => "London", 'Scotland' => "Edinburgh", 'France' => "Paris");
foreach ($capitals as $country=>$capital) {
echo "The capital of $country is $capital<br />";
}
?>
输出:
The capital of England is London
The capital of Scotland is Edinburgh
The capital of France is Paris
您正在尝试遍历一个不存在的数组,$capitals
。您必须遍历数组$country
。
foreach ($country as $country=>$capital) {
echo "The capital of $country is $capital";
}