如何创建td取决于三元运算符?

时间:2018-03-24 06:04:39

标签: php html-table codeigniter-3 ternary-operator

  $orientationData = explode('_', $generate_table['orientation']);
  foreach ($orientationData as $orientationData1) {
     $get_ori= $this->o->get_orientation($orientationData1);
     $table_row[$td++] = array('data' => $get_ori, 'style' => 'text-align:center;','class'=>'editable', $orientationData1>0?'id'=>$orientationData1);
  }

$orientationData就像1,2,3,4,0,0,0,8,9; 当给定'id'=>$orientationData1 id时,名称生成为1,2,3,4,0,0,0,8,9 我希望它在没有零的情况下生成

2 个答案:

答案 0 :(得分:0)

foreach ($orientationData as $orientationData1) {
    $get_ori= $this->o->get_orientation($orientationData1);
    if($orientationData1>0){
        $table_row[$td++] = array('data' => $get_ori, 'style' => 'text-align:center;','class'=>'editable','id'=>$orientationData1);
    }else{
        $table_row[$td++] = array('data' => $get_ori, 'style' => 'text-align:center;','class'=>'editable');
    }
}

这些结果还好..请告诉我另一种方法?

答案 1 :(得分:0)

您可以使用array_filter()http://php.net/array_filter)从数组中删除0。如果您没有为该功能提供回叫,则会删除“0”和“虚假”#39;值。

  $orientationData = explode('_', $generate_table['orientation']);
  $orientationData = array_filter($orientationData);
  foreach ($orientationData as $orientationData1) {
     $get_ori= $this->o->get_orientation($orientationData1);
     $table_row[$td++] = array('data' => $get_ori, 'style' => 'text-align:center;','class'=>'editable', $orientationData1>0?'id'=>$orientationData1);
  }