drrog9853 2017-12-22 23:11
浏览 31
已采纳

使用多维数组创建输入元素

I have a list of <input> elements and I am trying to simplify it so the code is tidier and cleaner. Here is what I have at the moment:

if($row['replies'] == '1') {
   echo'<input type="submit" value="Locked">';
}else{
   echo'<input type="submit" value="Lock">';
}
if($row['flagged'] == '1') {
   echo'<input type="submit" value="Ignore">';
}
if($row['deleted'] == '1') {
   echo'<input type="submit" value="Deleted">';
}else{
   echo'<input type="submit" value="Delete">';
}
<input type="submit" value="Hello">
<input type="submit" value="Goodbye">

I am trying to simplify this using a multidimensional array, but I can't quite get my head around it. This is what I've tried so far:

$options = array("Lock"=>array("replies","Locked"),"Ignore"=>"flagged","Delete"=>array("deleted","Deleted"),"Hello","Goodbye");
foreach($options as $option) {
   if(in_array($option[0], array('Lock','Deleted'), true) && $row[$option[1] == '1') {
      echo'<input type="submit" value="'.$option[2].'">';
   }else{
      echo'<input type="submit" value="'.$option[0].'">';
   }
}
  • 写回答

2条回答 默认 最新

  • doutucui0133 2017-12-22 23:44
    关注
    $submit_values =
    [
        $row['replies'] == '1' ? 'locked' : 'lock',
        $row['flagged'] == '1' ? 'ignore' : '',
        $row['deleted'] == '1' ? 'deleted' : 'delete',
        'hello',
        'goodbye'
    ];
    $submit_values = array_filter($submit_values); // Remove any empty values.
    foreach($submit_values as $value)
        printf ('<input type="submit" value="%s">', $value);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路