这个php数组有什么问题?

时间:2009-07-30 22:00:04

标签: php arrays

$canada_territories = array("British Columbia" => "British Columbia",  "Manitoba" => "Manitoba", "New Brunswick" => "New Brunswick", "Newfoundland and Labrador" => "Newfoundland and Labrador", "Nova Scotia" => "Nova Scotia", "Northwest Territories" => "Northwest Territories", "Nunavut" => "Nunavut", "Ontario" => "Ontario", "Prince Edward Island" => "Prince Edward Island", "Quebec" => "Quebec", "Saskatchewan" => "Saskatchewan", "Yukon" => "Yukon");
print_r($canada_territories);

结束

Array ( [British Columbia [Manitoba]] => British Columbia [Manitoba] => Manitoba [New Brunswick] => New Brunswick [Newfoundland and Labrador] => Newfoundland and Labrador [Nova Scotia] => Nova Scotia [Northwest Territories] => Northwest Territories [Nunavut] => Nunavut [Ontario] => Ontario [Prince Edward Island] => Prince Edward Island [Quebec] => Quebec [Saskatchewan] => Saskatchewan [Yukon] => Yukon )

ritish Columbia [曼尼托巴]是通过添加下一个数组项目的一部分而开始出错的地方

造成这种情况的原因是什么?

我正在尝试构建这个数组以填充下拉表单,我对阵列知之甚少,我是否真的需要将不列颠哥伦比亚省添加到阵列两次才能循环通过它们?

这是我通常使用的,但它通常有我设置的键和值,在这种情况下,我需要在我通常显示2个不同值的地方显示两倍的值

foreach ($state_array as $sid => $statename) {
      print '<option value=' . $sid . $selected . '>' . $statename . '</option>';
}

更新

我没有用标签包装,它显示我在chrome中发布的内容,当我查看来源它实际上是正确的

6 个答案:

答案 0 :(得分:2)

您发布的数组规范或输出没有任何问题。我怀疑你只是误解了print_r()结果。

但是,要填充下拉列表,您只需要:

$canada_territories = array( 
    "British Columbia",  
    "Manitoba",
    "New Brunswick", 
    "Newfoundland and Labrador",
    "Northwest Territories",
    "Nunavut",
    "Ontario",
    "Prince Edward Island",
    "Quebec",
    "Saskatchewan",
    "Yukon",
);
print_r($canada_territories);

也许这不会让人感到困惑。

答案 1 :(得分:1)

我认为数组很好,标签就是问题。

你有:

foreach ($canada_territories as $sid => $statename) {
  print '<option value=' . $sid . $selected . '>' . $statename . '</option>' . "\n";
}

由于没有引号,浏览器会在值结束后使用第一个空格。这是由此导致的错误HTML:

<option value=British Columbia>British Columbia</option>
<option value=Manitoba>Manitoba</option>
<option value=New Brunswick>New Brunswick</option>
<option value=Newfoundland and Labrador>Newfoundland and Labrador</option>
<option value=Nova Scotia>Nova Scotia</option>
<option value=Northwest Territories>Northwest Territories</option>
<option value=Nunavut>Nunavut</option>
<option value=Ontario>Ontario</option>
<option value=Prince Edward Island>Prince Edward Island</option>
<option value=Quebec>Quebec</option>
<option value=Saskatchewan>Saskatchewan</option>
<option value=Yukon>Yukon</option>

试试这个:

foreach ($canada_territories as $sid => $statename) {
  print "<option value=\"{$sid}\" $selected>{$statename}</option>\n";
}

产生这个:

<option value="British Columbia" >British Columbia</option>
<option value="Manitoba" >Manitoba</option>
<option value="New Brunswick" >New Brunswick</option>
<option value="Newfoundland and Labrador" >Newfoundland and Labrador</option>
<option value="Nova Scotia" >Nova Scotia</option>
<option value="Northwest Territories" >Northwest Territories</option>
<option value="Nunavut" >Nunavut</option>
<option value="Ontario" >Ontario</option>
<option value="Prince Edward Island" >Prince Edward Island</option>
<option value="Quebec" >Quebec</option>
<option value="Saskatchewan" >Saskatchewan</option>
<option value="Yukon" >Yukon</option>

答案 2 :(得分:0)

如果使用单引号而不是双引号,是否会出现问题?

很好地参考了解Array函数=&gt; http://us2.php.net/manual/en/function.array.php

答案 3 :(得分:0)

我没有看到这个:

$ php
<?php 

print_r(array("British Columbia" => "British Columbia",  "Manitoba" => "Manitoba", "New Brunswick" => "New Brunswick", "Newfoundland and Labrador" => "Newfoundland and Labrador", "Nova Scotia" => "Nova Scotia", "Northwest Territories" => "Northwest Territories", "Nunavut" => "Nunavut", "Ontario" => "Ontario", "Prince Edward Island" => "Prince Edward Island", "Quebec" => "Quebec", "Saskatchewan" => "Saskatchewan", "Yukon" => "Yukon")); 

Array
(
    [British Columbia] => British Columbia
    [Manitoba] => Manitoba
    [New Brunswick] => New Brunswick
    [Newfoundland and Labrador] => Newfoundland and Labrador
    [Nova Scotia] => Nova Scotia
    [Northwest Territories] => Northwest Territories
    [Nunavut] => Nunavut
    [Ontario] => Ontario
    [Prince Edward Island] => Prince Edward Island
    [Quebec] => Quebec
    [Saskatchewan] => Saskatchewan
    [Yukon] => Yukon
)

您确定要以真实的方式输出结果吗?

答案 4 :(得分:0)

这里打印得很好。

<?php
$canada_territories = array("British Columbia" => "British Columbia",  "Manitoba" => "Manitoba", "New Brunswick" => "New Brunswick", "Newfoundland and Labrador" => "Newfoundland and Labrador", "Nova Scotia" => "Nova Scotia", "Northwest Territories" => "Northwest Territories", "Nunavut" => "Nunavut", "Ontario" => "Ontario", "Prince Edward Island" => "Prince Edward Island", "Quebec" => "Quebec", "Saskatchewan" => "Saskatchewan", "Yukon" => "Yukon");
print_r($canada_territories);
?>

输出

Array
(
    [British Columbia] => British Columbia
    [Manitoba] => Manitoba
    [New Brunswick] => New Brunswick
    [Newfoundland and Labrador] => Newfoundland and Labrador
    [Nova Scotia] => Nova Scotia
    [Northwest Territories] => Northwest Territories
    [Nunavut] => Nunavut
    [Ontario] => Ontario
    [Prince Edward Island] => Prince Edward Island
    [Quebec] => Quebec
    [Saskatchewan] => Saskatchewan
    [Yukon] => Yukon
)

答案 5 :(得分:0)

首先,为什么要强制数组的键和值为相同的值?好像你应该删除相关文本并让它以数字形式对其进行索引。

其次,单引号和双引号不是问题,尽管你应该使用单引号,这样你就不会强迫PHP查找不存在的$ vars(双引号的目的之一)。那里有很多文章:http://www.google.com/search?q=php+single+vs+double+quotes

最后,我没有得到您在运行代码时所做的结果:

Array
(
    [British Columbia] => British Columbia
    [Manitoba] => Manitoba
    [New Brunswick] => New Brunswick
    [Newfoundland and Labrador] => Newfoundland and Labrador
    [Nova Scotia] => Nova Scotia
    [Northwest Territories] => Northwest Territories
    [Nunavut] => Nunavut
    [Ontario] => Ontario
    [Prince Edward Island] => Prince Edward Island
    [Quebec] => Quebec
    [Saskatchewan] => Saskatchewan
    [Yukon] => Yukon
)