dongqi8030 2018-02-14 08:21
浏览 161
已采纳

使用多维数组重复选择optgroup标签

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 -->

image result

**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.

  • 写回答

1条回答 默认 最新

  • duanlan5320 2018-02-14 08:56
    关注

    You will need to perform iterated conditions so that the optgroup tags are correctly used/positioned.

    Code: (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>";
    

    Output:

    <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>
    

    ...add your styles as necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”