duanfu3884 2019-07-07 09:56
浏览 48
已采纳

合并下拉php中的重复值

I would like to merge duplicate values that i have in my drop down list, i dont want to use array_unique because it deletes my second duplicate value. The way my program works is if you select a location it will populate my second drop down accordingly. enter image description here

If you look at the picture provided you can see two duplicate entries Rosebank when i click the first rosebank it will only show me on provider in my second drop down but when i click on the second rosebank it shows me all the providers with the location rosebank. I would like to know if there is a way i can merge the two duplicates so that it will show me all the providers when i click rosebank and it should only show one rosebank.

Here is my code without array_unique:

    <label for="select-service">
                <strong>Enter a Location:</strong>
            </label>
            <select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
                <option value="">Select Location</option>



                 <?php


            foreach($appointment_locations as $location) {



             $LocationsArray = explode(",", $location->notes);

              foreach($LocationsArray as $singleLocation):
                 ?>

                <option value="<?=$singleLocation ?>"><?=$singleLocation  ?></option>                  

                <? endforeach;

                 };?>

            </select>

and here is what i have tried with array_unique, it successfully deletes the duplicates but as i said earlier, it will only show me one value in my next drop down and not all

  <label for="select-service">
                <strong>Enter a Location:</strong>
            </label>
            <select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
                <option value="">Select Location</option>



                 <?php

          foreach(array_unique($appointment_locations) as $location) {
           // foreach($appointment_locations as $location) {



             $LocationsArray = explode(",", $location->notes);

              foreach($LocationsArray as $singleLocation):
                 ?>

                <option value="<?=$singleLocation ?>"><?=$singleLocation  ?></option>                  

                <? endforeach;

                 };?>

            </select>

here is my var_dump

                         array(2) {
  [0]=>
  object(stdClass)#28 (2) {
    ["id"]=>
    string(1) "4"
    ["notes"]=>
    string(29) "Randburg, Greenside, Rosebank"
  }
  [1]=>
  object(stdClass)#29 (2) {
    ["id"]=>
    string(1) "8"
    ["notes"]=>
    string(8) "Rosebank"
  }
}
  • 写回答

2条回答 默认 最新

  • doujun7161 2019-07-09 17:07
    关注

    This should do it:

    Get the unique locations by storing them in an array as the keys:

    $unique_locations = [];
    foreach(array_unique($appointment_locations) as $location) {
        foreach(explode(",", $location->notes) as $singleLocation) {
            $unique_locations[$singleLocation] = 1;
        }
    }
    ?>
    

    Use array_keys, array_map and implode to put it all together:

    ...
    <select>
        <?= implode('', array_map(function($location) { return "<option value='$location'>$location</option>"; }, array_keys($unique_locations)))  ?>
    </select>
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测