我有多维数组,我想将它显示在带有Optgroup的Select标签中,但是反复显示optgroup。我需要的是将值分组到只显示一次的Optgroup。
我的数组
[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
)
)
)
我的代码
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>';
}
}
,结果是:
<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>
我有多维数组,我想将它显示在带有Optgroup的Select标签中,但是反复显示optgroup。我需要的是将值分组到只显示一次的Optgroup。
答案 0 :(得分:0)
您需要执行迭代条件,以便正确使用/定位optgroup标记。
代码:(Demo)
echo "<select>";
$optgroup=null;
foreach($linkxy as $item){
$label=key($item);
if($optgroup!==$label){
if(!is_null($optgroup)){
echo "</optgroup>"; // close before opening the next
}
echo "<optgroup label=\"$label\">"; // open to optgroup
$optgroup=$label;
}
$animal_data=current(current($item)); // drill down to access deep subarray
echo "<option value=\"{$animal_data['link']}\">{$animal_data['city']}</option>";
}
if(!is_null($optgroup)){
echo "</optgroup>"; // close the final dangling optgroup
}
echo "</select>";
输出:
<select>
<optgroup label="Cordillera Autonomous Region (CAR)">
<option value="/search/veterinarians/?vet&provid=1&animal=ZG9ncw==&keyword=Abra&keysearch=QWJyYQ==&c">Abra</option>
<option value="/search/veterinarians/?vet&provid=7&animal=ZG9ncw==&keyword=Apayao&keysearch=QXBheWFv&c">Apayao</option>
<option value="/search/veterinarians/?vet&provid=13&animal=ZG9ncw==&keyword=Benguet&keysearch=QmVuZ3VldA==&c">Benguet</option>
</optgroup>
<optgroup label="Region I">
<option value="/search/veterinarians/?vet&provid=35&animal=ZG9ncw==&keyword=Ilocos+Sur&keysearch=SWxvY29zIFN1cg==&c">Ilocos Sur</option>
<option value="/search/veterinarians/?vet&provid=60&animal=ZG9ncw==&keyword=Pangasinan&keysearch=UGFuZ2FzaW5hbg==&c">Pangasinan</option>
</optgroup>
<optgroup label="Region II">
<option value="/search/veterinarians/?vet&provid=11&animal=ZG9ncw==&keyword=Batanes&keysearch=QmF0YW5lcw==&c">Cagayan</option>
</optgroup>
</select>
...根据需要添加你的风格。