I have multidimentional array and I want to display it into Select tags w/ Optgroup but the optgroup display repeatedly. what I need is to group the the values into Optgroup that show only once.
My Array
[0] => Array (
[Cordillera Autonomous Region (CAR)] => Array (
[dogs] => Array (
[link] => /search/veterinarians/?vet&provid=1&animal=ZG9ncw==&keyword=Abra&keysearch=QWJyYQ==&c
[city] => Abra
)
)
[1] => Array (
[Cordillera Autonomous Region (CAR)] => Array (
[dogs] => Array (
[link] => /search/veterinarians/?vet&provid=7&animal=ZG9ncw==&keyword=Apayao&keysearch=QXBheWFv&c
[city] => Apayao
)
)
[2] => Array (
[Cordillera Autonomous Region (CAR)] => Array (
[dogs] => Array (
[link] => /search/veterinarians/?vet&provid=13&animal=ZG9ncw==&keyword=Benguet&keysearch=QmVuZ3VldA==&c
[city] => Benguet
)
.
.
.
.
[7] => Array (
[Region I] => Array (
[dogs] => Array (
[link] => /search/veterinarians/?vet&provid=35&animal=ZG9ncw==&keyword=Ilocos+Sur&keysearch=SWxvY29zIFN1cg==&c
[city] => Ilocos Sur
)
)
)
.
.
[9] => Array (
[Region I] => Array (
[dogs] => Array (
[link] => /search/veterinarians/?vet&provid=60&animal=ZG9ncw==&keyword=Pangasinan&keysearch=UGFuZ2FzaW5hbg==&c
[city] => Pangasinan
)
)
)
[10] => Array (
[Region II] => Array (
[dogs] => Array (
[link] => /search/veterinarians/?vet&provid=11&animal=ZG9ncw==&keyword=Batanes&keysearch=QmF0YW5lcw==&c
[City] => Cagayan
)
)
)
My Code
for($i=0; $i<=count($linkxy);$i++){
$z = $linkxy[$i];
foreach($z as $keyx=>$valuesxy){
$animalLink = $valuesxy[$animalfamily]['link'];
$animalCity = $valuesxy[$animalfamily]['city'];
$animalRegion = $valuesxy[$animalfamily]['region'];
echo '<optgroup label="'.$keyx.'" '.$style.'>';
echo '<option value="'.$animalLink.'" '.$style.' >'.$animalCity.'';
echo '</option>';
echo '</optgroup>';
}
}
and the result is:
<select>
<optgroup label="Cordillera Autonomous Region (CAR)">
<option>Abra</option>
</optgroup>
<optgroup label="Cordillera Autonomous Region (CAR)">
<option>Apayao</option>
</optgroup>
<optgroup label="Cordillera Autonomous Region (CAR)">
<option>Benguet</option>
</optgroup>
<!-- and so on -->
**the result I want is:**
<select>
<optgroup label="Cordillera Autonomous Region (CAR)">
<option>Abra</option>
<option>Apayao</option>
<option>Benguet</option>
</optgroup>
<optgroup label="Region I">
<option>Ilocos Sur</option>
</optgroup>
<optgroup label="Region II">
<option>Cagayan</option>
</optgroup>
</select>
I have multidimentional array and I want to display it into Select tags w/ Optgroup but the optgroup display repeatedly. what I need is to group the the values into Optgroup that show only once.