tags:

views:

103

answers:

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

Ends up being

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 [Manitoba] is where it starts to go wrong by adding part of the next array item

What is causing this?

I am trying to build this array to populate a dropdown form,i don't know enough about array, do I even really need to add British Columbia to the array twice to be able to cycle through them?

This is what I usually use but it usually has a key and value that I set, on this case I need to show the value twice below where I usually show 2 different values

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

UPDATE

I wasnt wrapping with tags and it showed up how I posted in chrome, when I view source it is actually correct

A: 

Does the problem happen if you use single quotes instead of double?

Good reference to understand the Array function => http://us2.php.net/manual/en/function.array.php

CodeToGlory
+2  A: 

There's nothing wrong with the array specification you're posting, or the output. I suspect you're just misreading the print_r() results somehow.

For populating a dropdown, though, all you need is:

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

Maybe that will be less confusing.

chaos
A: 

I do not see this:

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

Are you sure you're outputting your results in a way that shows you truly what you have?

Crad
Ahh I wasnt wrapping with <pr> tags and it showed up how I posted in chrome, when I view source it is actually correct
jasondavis
A: 

It prints just fine here.

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

Outputs

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
)
Aistina
A: 

First, why are you forcing the key and value of the array to be the same value? Seems like you should drop the associated text and let it index it numerically.

Second, single vs. double quotes is not the issue here, though you should be using single quotes so you don't force PHP to look for $vars that aren't there (one of the purposes of double quotes). lots of articles out there on that: http://www.google.com/search?q=php+single+vs+double+quotes

Lastly, I did not get the results you did when running your code at all:

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
)
xentek
yes I updated, it was just the way my browser was showing it without pre tags, thanks
jasondavis
+1  A: 

I think the array is fine, it's the tags that are the problem.

You have:

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

Which messes up because without quotes, the browser assumes the first space after value ends the value. Here's the resulting bad 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>

Try this:

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

Which generates this:

<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>
artlung