drq9991 2013-08-15 22:42
浏览 181
已采纳

如何在数组中使用三元运算符?

I am trying to figure out how I can use the ternary operator in my array. I want to be able to check if the value matches then make that option "selected".

Here is my code:

<select id="time_out_1" name="data[<?= $id;?>][time_out]">

<?php

$miltimes = array("<option value=\"0000\">0000</option>","<option value=\"0030\">0030</option>",   "<option value=\"0100\">0100</option>",   "<option value=\"0130\">0130</option>",   "<option value=\"0200\">0200</option>",   "<option value=\"0230\">0230</option>",   
"<option value=\"0300\">0300</option>",   "<option value=\"0330\">0330</option>",   "<option value=\"0400\">0400</option>",  "<option value=\"0430\">0430</option>",   "<option value=\"0500\">0500</option>",   "<option value=\"0530\">0530</option>",   "<option value=\"0600\">0600</option>",   "<option value=\"0630\">0630</option>",   "<option value=\"0700\">0700</option>",   "<option value=\"0730\">0730</option>",   "<option value=\"0800\">0800</option>",   "<option value=\"0830\">0830</option>",   "<option value=\"0900\">0900</option>",   "<option value=\"0930\">0930</option>",   "<option value=\"1000\">1000</option>",   "<option value=\"1030\">1030</option>",   
"<option value=\"1100\">1100</option>",   "<option value=\"1130\">1130</option>",   "<option value=\"1200\">1200</option>",   "<option value=\"1230\">1230</option>",   "<option value=\"1300\">1300</option>",   "<option value=\"1330\">1330</option>",   "<option value=\"1400\">1400</option>",   "<option value=\"1430\">1430</option>",   "<option value=\"1500\">1500</option>",   "<option value=\"1530\">1530</option>",   "<option value=\"1600\">1600</option>",   "<option value=\"1630\">1630</option>",   "<option value=\"1700\">1700</option>",   "<option value=\"1730\">1730</option>",   "<option value=\"1800\">1800</option>",   "<option value=\"1830\">1830</option>",   "<option value=\"1900\">1900</option>",   "<option value=\"1930\">1930</option>",   "<option value=\"2000\">2000</option>", "<option value=\"2030\">2030</option>",      "<option value=\"2100\">2100</option>",      "<option value=\"2130\">2130</option>",      "<option value=\"2200\">2200</option>",      "<option value=\"2230\">2230</option>",      "<option value=\"2300\">2300</option>",      "<option value=\"2330\">2330</option>");

foreach($miltimes as $miltime) { echo $miltime; } 
echo '</select>';?>

I found this answer but I am not quite sure how to use it in my situation.

Using an If-else within an array

Thanks in advance for the help.

  • 写回答

2条回答 默认 最新

  • doufei8250 2013-08-16 10:46
    关注

    Instead of putting all your options tags in the array, you can just put the numbers and use PHP to create the tags for you:

    $miltimes = array('0000', '0030', '0100', '0130', '0200', '0230', '0300', '0330', '0400', '0430', '0500', '0530', '0600', '0630', '0700', '0730', '0800', '0830', '0900', '0930', '1000', '1030', '1100', '1130', '1200', '1230', '1300', '1330', '1400', '1430', '1500', '1530', '1600', '1630', '1700', '1730', '1800', '1830', '1900', '1930', '2000', '2030', '2100', '2130', '2200', '2230', '2300', '2330');
    $select = '<select id="time_out_1" name="'.$data[$id]['time_out']."\">
    ";
    foreach ($miltimes as $key => $value){
        $selected = ($value == '0030') ? ' selected' : '';
        $select .= "\t<option value=\"$value\"$selected>$value</option>
    ";
    }
    $select .= "</select>
    ";
    echo $select;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!