【问题标题】:Drawing empty dummy columns on loop在循环上绘制空的虚拟列
【发布时间】:2019-04-28 21:18:01
【问题描述】:

考虑将以下数据数组分配给“$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>';
}

【问题讨论】:

    标签: php css html arraylist multidimensional-array


    【解决方案1】:
    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>';
        }
    }
    

    【讨论】:

    • 行和行尾 div 将照常进行。
    【解决方案2】:

    基于你总是希望有 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>';
    }
    

    【讨论】:

    • 这可行,但我不知道为什么某些列没有写入值。但是定位效果很好。我还在 $drawColumns = [0,0,0,0]; 顶部添加了一个
      将继续使用此代码检查为什么有空白数据。有点奇怪。总是有空白数据。我在 10 个不同的数据数组上对其进行了测试。
    • 我刚刚更新了我的答案。问题是我用来从 $row 获取值的索引。因为 $x 不等于该列最初在 $my_array 中的索引。
    【解决方案3】:
    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>';
    }
    

    【讨论】:

    • 代码无效。它运行并提供了一些不错的价值,但它与数据数组不相符。
    • “但它与数据数组不相符” - 你是什么意思?我用您提供的样本数据对其进行了测试,效果很好。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签