multidimension array from string to optgroup

Hi I am new to php and hoping someone can help me
I have a variable which contains a string like this:
[php]location_x=’[Country],county1,county2,county3,[Country2],county1,county2,county3,[Country3],county1,county2,county3’;[/php]

I can explode it and get select options ok like this- but I want to make it so country is a optgroup value rather than an option - I think I need to somehow make my array into a multideminsional one? Any help would be greatly apprecaited.

Cheers

[php]
$x = explode(’,’,$location_x);
for($i=0;$i<count($x);$i++)
{
if(trim($x[$i]))
{
echo ‘<option ‘;
if(trim($x[$i])==$location_id)
{
echo ’ selected=“selected” ‘;
}
echo ‘value="’. trim($x[$i]).’">’. trim($x[$i]).’’;
}
}
}
[/php]

If countries in your list are always surrounded with square brackets, and both countries and counties are already sorted properly, you can just modify your script like this:

[php]<?php
$x = explode(’,’,$location_x);
for($i=0;$i<count($x);$i++){
if(trim($x[$i])){
if(substr(trim($x[$i]),0,1)==’[’ and substr(trim($x[$i]),-1)==’]’){ // country - option group
echo ‘’;
}
else{ // county - regular option
echo ‘<option ‘;
if(trim($x[$i])==$location_id) echo ’ selected=“selected” ‘;
echo ‘value="’. trim($x[$i]).’">’. trim($x[$i]).’’;
}
}
}
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service