php数组加上foreach语法

时间:2013-05-09 04:47:01

标签: php syntax

我在编写以下语法php语法时遇到问题: -

简单数组语法:

$array = array(
"a" => 1,
"b" => 2,
"c" => 3
)

简单的foreach语法:

foreach ($countries as $key => $value){
echo $key; echo $value;
}

现在,我想结合以上2: -

$array = array{
foreach ($countries as $key => $value){
 $key => $value,
}
}

我怎么写呢?或者我只能构造一个字符串看起来像数组括号内的单词?

2 个答案:

答案 0 :(得分:6)

你应该写

foreach ($countries as $key => $value){
    $array[$key] = $value;
}

答案 1 :(得分:0)

请尝试以下代码

$array =$array+$country;

print_r($array);