在循环上绘制空的虚拟列

时间:2018-11-27 07:13:50

标签: php css html5 arraylist multidimensional-array

请考虑将以下数据数组分配给“ $ my_array”。我已经根据本文底部的数据提供了预期输出的屏幕截图。

注意:由于某些原因,我无法修改数据库上数据的插入,因此我需要基于此结构绘制空列。

Array
(
[1] => Array //This is row #1
    (
        [0] => Array
            (
                [0] => column 2 text
                [1] => column2 //column identifier
            )

        [1] => Array
            (
                [0] => column 3 text
                [1] => column3 //column identifier
            )

        [2] => Array
            (
                [0] => column 4 text
                [1] => column4 //column identifier
            )
    )

[2] => Array //This is row #2
    (
        [0] => Array
            (
                [0] => column 1 text
                [1] => column1 //column identifier
            )

        [1] => Array
            (
                [0] => column 4 text
                [1] => column4 //column identifier
            )

    )
)

这是一件事:

无论行返回1、2或3列数据,我都希望在每行上绘制4列。我能够在应绘制列的字段上添加一个标识符。

到目前为止,我是这样的: 注意:我真的不知道如何确定某些列是否不在行数据上,并添加空白空间,这就是我到目前为止所要做的:

foreach($my_array as $row) {
    echo '<div class="row">'; //draw the rows

    //I used for loop instead of foreach to create four columns
    for($x = 0; $x < 4; $x++) {
        //draw the column here

        //write column text data $row[$x][0]
    }

    echo '</div>';
}

enter image description here

3 个答案:

答案 0 :(得分:1)

基于您始终希望有4列的情况,可以按照以下方式进行操作:

$columns = ['column1','column2','column3','column4'];        // create list to check for identifier
foreach($my_array as $row) {
  echo '<div class="row">';
  $drawColumns = [0,0,0,0];                                  // if 0, col empty, if 1 data exist
  $columnIndex = [0,0,0,0];
  foreach($row as $index => $column) {                                  // loop over row to fill $drawColumns 
    $columnPosition = array_search($column[1],$columns);     // array_search gives index of the found entry
    $drawColumns[$columnPosition] = 1;                       // mark column as not empty
    $columnIndex[$columnPosition] = $index;         // store the original index of the column in the row
  }
  for($x = 0; $x < 4; $x++) {                                // just loop over drawColumns 
    if ($drawColumns[$x] == 1) {                             // draw data if exists
      echo '<div class="col">'.$row[$columnIndex[$x]][0].'</div>';
    } else {                                                 // else draw empty column
      echo '<div class="col">empty column</div>';
  }
  echo '</div>';
}

答案 1 :(得分:0)

foreach($row as $r){
    $colid[] = $r['1'];
}
for($x = 0; $x < 4; $x++) {
    if(in_array($row[$x]['1'],$colid)){
        echo '<div class="col-md-4">'.$row[$x]['1'].'</div>';
    } else {
        echo '<div class="col-md-4">Empty Cell</div>';
    }
}

答案 2 :(得分:0)

foreach($my_array as $row) {
    echo '<div class="row">';

    for($x = 1; $x <= 4; $x++) {
      $colval = '-'; // default value to output for empty column;
                     // you can also use an empty string or other placeholder
      foreach($row as $val) {
        if( $val[1] == 'column'.$x ) {
          $colval = $val[0]; // take this value, if entry with columnX exists
          break;
        }
      }
      echo '<div class="col">', $colval, '</div>';
    }

    echo '</div>';
}