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

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

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;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsmupo6631 2013-08-15 22:51

    If you are trying to set a time based on the values and if your time values are strings then

    $matchtime = "1000";
    $defaulttime = "0500"
    
    foreach($miltimes as $miltime) { 
        $timematched = ($miltime == $matchtime) ? $miltime : $defaulttime;
        echo $timematched; 
    }
    

    Although it is better to have only time values in your array and to use a built-in function like strtotime when comparing time. That way you wont be making string comparisons.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题