Page 1 of 1

array

Posted: Mon Jun 16, 2008 3:40 am
by pedroz
$countries = array (
"london,united kingdom,",
"manchester,united kingdom",
"madrid,spain",
"lisbon,portugal"
);

With the array above, I would like to display the following

-united kingdom
london
manchester

-spain
madrid

-portugal
lisbon

thanks :)

Re: array

Posted: Mon Jun 16, 2008 4:25 am
by Kieran Huggins
You'll need to generate a temp array like:

Code: Select all

$countries = Array(
  "United Kingdom" => Array(
    "Manchester",
    "London"
  ),
  // an so on
)
then iterate over that.

Re: array

Posted: Mon Jun 16, 2008 8:23 am
by Ambush Commander

Code: Select all

$countries = array (
"london,united kingdom,",
"manchester,united kingdom",
"madrid,spain",
"lisbon,portugal"
);
$country_city = array();
 
foreach($countries as $city_string) {
  list($city, $country) = explode(',', $city_string,2);
  $country_city[$country][] = $city;
}
 
Displaying this is left as an exercise to the reader.