【问题标题】:php dynamic table from muldimentional array来自多维数组的php动态表
【发布时间】:2020-10-02 07:50:20
【问题描述】:

你能告诉我如何为“动态”html 表实现这个想法吗? 我有一个数组

$arr = array(
  array(
       'label' => 'First name',
       'data' => array(
           array('fname' => 'John'),
           array('fname' => 'Ralph'),
       ),
  ),
  array(
       'label' => 'Last name',
       'data' => array(
           array('lname' => 'Doe'),
           array('lname' => 'Loren'),
       ),
  ),
  array(
       'label' => 'Description',
       'data' => array(
           array('description' => 'Something bout John'),
           array('description' => 'Something about Ralph'),
       ),
  ),
);

现在从键“标签”即时创建表格列

---------------------------------------
|First Name | Last Name | Description |
---------------------------------------

问题是如何将“fname”键放在第一列,“lname”放在第二列,“description”放在第三列。

通过这部分代码,我试图将数据放在所有列中

    private function tableBody()
{
    $data = $this->data;
    $table = '<tbody>';

    foreach($data as $key => $value){
        foreach($value['data'] as $k => $v){
            $table .= '<tr>';
            foreach($v as $col => $name){
                $table .= '<td>' . $name . '</td>';
            }
            $table .= '</tr>';
        }
    }

    $table .= '</tbody>';

    return $table;
}

另外我的想法不是硬编码数组键以供多次使用(标签和数据除外)。

【问题讨论】:

    标签: php arrays dynamic


    【解决方案1】:

    首先我会重新映射数据以循环处理它。

    $labels = [];
    $rows = [];
    foreach($arr as $column) {
        $label = $column['label'];
        $labels[] = $label;
        foreach($column['data'] as $key => $value) {
            $rows[$label][] = $value;
        }
    }
    
    

    现在打印出带有标题的标签。

    echo "<table>\n";
    echo "<tr>";
    foreach($labels as $label) echo "<th>{$label}</th>";
    echo "</tr>\n";
    

    遍历行并创建 HTML。

    $labelCount = count($labels);
    $rowCount = count($rows[$labels[0]]);
    
    while($rowCount) {
        echo "<tr>";
        for ($i = 0; $i < $labelCount; $i++) {
            $current = array_shift($rows[$labels[$i]]);
            $value = reset($current);
            echo "<td>{$value}</td>";
        }
        echo "</tr>\n";
        $rowCount--;
    }
    echo "</table>\n";
    

    这给出了如下表

    <table>
    <tr><th>First name</th><th>Last name</th><th>Description</th></tr>
    <tr><td>John</td><td>Doe</td><td>Something bout John</td></tr>
    <tr><td>Ralph</td><td>Loren</td><td>Something about Ralph</td></tr>
    </table>
    

    【讨论】:

    • 是的,它奏效了。谢谢你。我有这个想法,但我认为我做不到。
    • 也许有更好的解决方案,但我认为最好的方法是让数据以易于迭代的形式存在。因此,在我看来,重新映射是一种可读的方式。至少它看起来更容易,而不是处理“可统一”的数据集。
    【解决方案2】:

    您需要操纵初始数组以将其减少到更易于管理的状态。你可以像这样暴力破解:

      function reduce($data) {
          $ret = [];
          foreach ($data as $d1) {
              // column header could have been fetched here
              foreach ($d1 as $k2 => $d2) {
                // keeping track of what label we need
                $key = '';
                // keeping the values together
                $child = [];
                  // discarding non arrays
                  if (is_array($d2)) {
                      foreach ($d2 as $d3) {
                          // identifier fetch from this level
                          foreach ($d3 as $k4 => $d4) {
                            $key = $k4;
                            $child[] = $d4;
                          }
                      }
                  }
                  // assigning back to the main array only if we have something
                  if (!empty($child)) {
                    $ret[$key] = $child;
                  }
              }
          }
    
          return $ret;
      }
    

    这将返回以下内容:

    Array
    (
        [fname] => Array
            (
                [0] => John
                [1] => Ralph
            )
    
        [lname] => Array
            (
                [0] => Doe
                [1] => Loren
            )
    
        [description] => Array
            (
                [0] => Something bout John
                [1] => Something about Ralph
            )
    
    )
    

    【讨论】:

    • 我想我以类似的形式转换初始数组。
    猜你喜欢
    • 2019-10-26
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多