2009-07-30 139 views
0
$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); 

最終被這個php數組有什麼問題?

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哥倫比亞[馬尼托巴]是它開始猶豫了,加入下一個數組項

是什麼原因造成的一部分嗎?

我想建立這個數組來填充一個下拉表單,我對數組還不夠了解,我甚至真的需要將兩個不列顛哥倫比亞數組添加到數組中以便能夠循環遍歷它們嗎?

這是我經常使用,但它通常具有鍵和值,我設置,在這種情況下,我需要顯示以下值的兩倍,我通常表現出2個不同的值

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

UPDATE

我不是包裝與標籤,它顯示了我如何張貼在鍍鉻,當我查看源代碼,它實際上是正確的

+0

在這種情況下,因爲你不需要鑰匙那麼你可以定義數組沒有密鑰,因爲混亂。在這種情況下,如果使用foreach($ array作爲$ key => $ array),那麼$ key將是整數數組索引(因爲如果不指定鍵,PHP會自動添加從零開始的數組索引)。 – 2009-07-30 22:17:58

回答

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); 

也許這將是減少混亂。

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 
) 

你確定你是在輸出,顯示你真正的你有什麼辦法你的結果?

+0

Ahh我沒有用標籤打包,它顯示了我如何張貼在Chrome中,當我查看源代碼時它實際上是正確的 – JasonDavis 2009-07-30 22:12:52

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 
) 
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 
) 
+0

是的,我更新了,這只是我的瀏覽器顯示它沒有預先標籤的方式,謝謝 – JasonDavis 2009-07-30 22:14:39

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>