dougou2937 2017-06-04 05:04
浏览 35
已采纳

PHP - HTML选择菜单 - 包括数组之外的值

I'm creating a simple HTML Select Menu using PHP from an array as follows:

$output = '';

foreach($symptoms as $symptom) {

    $symptom = htmlspecialchars($symptom);
    if ($symptom == $record->getField('PatientSymptom')) {
      $output = "<option value='".$symptom."' selected='selected' >$symptom</option>";  
    } else {
      $output = "<option value='".$symptom."'>$symptom</option>";  
    }
    echo $output;
}

This creates the HTML select menu from the $symptoms array successfully, but the original value stored in the input field that I'm displaying might not be part of the $symptoms array. I need to add this to the select menu in this case but not sure the best way to go about this.

I'm storing the original value in a variable: $patientSymptom

What's the best way to check if $patientSymptom isn't in the $symptoms array and add that to the select menu I'm creating?

  • 写回答

1条回答 默认 最新

  • dpoppu4300 2017-06-04 05:16
    关注

    Use in_array() function:

    in_array — Checks if a value exists in an array


    Check if element exists in array right before the loop and add it if necessary:

    $output = '';
    if (!in_array($patientSymptom, $symptoms)) {
       $symptoms[] = $patientSymptom; // add symptom to end of the array
    }
    foreach($symptoms as $symptom) {
    // ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?