关联数组+ Foreach语句 - 未定义的变量

Mo *_*tin 0 php arrays foreach

我真的在努力做这项工作,我看过导游,但我似乎无法看到我和他们之间的区别,除了阵列的布局.

<?php
$country = array('England' => "London", 'Scotland' => "Edinburgh", 'France' => "Paris");

foreach ($capitals as $country=>$capital) {
echo "The capital of $country is $capital";
}

?>
Run Code Online (Sandbox Code Playgroud)

我想要它做的就是说这个国家和它的首都.

错误代码 -

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
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Riz*_*123 5

将您的代码更改为:

(我将数组名称从更改countrycapitals)

<?php

    $capitals = array('England' => "London", 'Scotland' => "Edinburgh", 'France' => "Paris");

    foreach ($capitals as $country=>$capital) {
        echo "The capital of $country is $capital<br />";
    }

?>
Run Code Online (Sandbox Code Playgroud)

输出:

The capital of England is London
The capital of Scotland is Edinburgh
The capital of France is Paris
Run Code Online (Sandbox Code Playgroud)