Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I've a question about an array and using foreach

Status
Not open for further replies.

FSEdge

Technical User
May 26, 2001
63
US
It's to my understanding that the code below should produce a list of towns and their population.

When I run the php page all that is returned is a single town and it's population. Shouldn't the foreach walk trough the $town_population array beginning to end and list all of it's data? If you haven't guessed already, I’m new to the php.


Code:
$town_population = array("Small Town" => 100,
						 "Medium Town" => 500, 
						 "Large Town" => 1000, 
						 "Mudville" => 1000000
						 );


ksort($town_population);
	foreach($town_population as $town => $population);
	{
		$population = number_format($population);
		echo "$town Population is: $population<br>";
	}

Thanks to any and all for help.

Keyboardingly yours,
FSEdge
 
Code:
foreach($town_population as $town => $population)
    {
        $population = number_format($population);
        echo "$town_population[$town] Population is: $population<br>";
    }

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
My mistake:

its:

Code:
foreach($town_population as $town => $population);[red]<< Just remove this semi colon[/red]
    {
        $population = number_format($population);
        echo "$town Population is: $population<br>";
    }

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top