Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP以表格格式显示数组_Php_Mysql_Arrays_Html Table - Fatal编程技术网

PHP以表格格式显示数组

PHP以表格格式显示数组,php,mysql,arrays,html-table,Php,Mysql,Arrays,Html Table,我有一个如下所示的数组,我需要将该数组显示到表中。我使用PHP尝试了该函数,但它获得了行和单元格值,我需要三种类型的值: 行名称 列名 对应行和列的名称 注意:我已将下面的数组1转换为数组2 数组1: Array ( [0] => Array ([id] => 1[rows] => R1[columns] => c1[name] => 1) [1] => Array([id] => 2[rows] => R1[columns] =&

我有一个如下所示的数组,我需要将该数组显示到表中。我使用PHP尝试了该函数,但它获得了行和单元格值,我需要三种类型的值:

  • 行名称
  • 列名
  • 对应行和列的名称
  • 注意:我已将下面的数组1转换为数组2

    数组1:

    Array
    (
        [0] => Array ([id] => 1[rows] => R1[columns] => c1[name] => 1)
        [1] => Array([id] => 2[rows] => R1[columns] => c2[name] => 2)
        [2] => Array([id] => 3[rows] => R1[columns] => c3[name] => 3)
        [3] => Array([id] => 4[rows] => R2[columns] => c1 [name] => 1)
        [4] => Array([id] => 5[rows] => R2[columns] => c2[name] => 2)
        [5] => Array([id] => 6[rows] => R2[columns] => c3[name] => 3)
    )
    
     Array
            (    
              [0] => Array([0] => R1[1] => c1[2] => 1)
              [1] => Array([0] => R1[1] => c2[2] => 2)
              [2] => Array([0] => R1[1] => c3[2] => 3)
              [3] => Array([0] => R2[1] => c1[2] => 1)
              [4] => Array([0] => R2[1] => c2[2] => 2)
              [5] => Array([0] => R2[1] => c3[2] => 3)
            )
    
    数组2:

    Array
    (
        [0] => Array ([id] => 1[rows] => R1[columns] => c1[name] => 1)
        [1] => Array([id] => 2[rows] => R1[columns] => c2[name] => 2)
        [2] => Array([id] => 3[rows] => R1[columns] => c3[name] => 3)
        [3] => Array([id] => 4[rows] => R2[columns] => c1 [name] => 1)
        [4] => Array([id] => 5[rows] => R2[columns] => c2[name] => 2)
        [5] => Array([id] => 6[rows] => R2[columns] => c3[name] => 3)
    )
    
     Array
            (    
              [0] => Array([0] => R1[1] => c1[2] => 1)
              [1] => Array([0] => R1[1] => c2[2] => 2)
              [2] => Array([0] => R1[1] => c3[2] => 3)
              [3] => Array([0] => R2[1] => c1[2] => 1)
              [4] => Array([0] => R2[1] => c2[2] => 2)
              [5] => Array([0] => R2[1] => c3[2] => 3)
            )
    
    表:

        id | row | column | value
        --------------------------
        1  | r1  |  c1    |  1
        2  | r1  |  c2    |  2
        3  | r1  |  c3    |  3
        4  | r2  |  c1    |  1
        5  | r2  |  c2    |  2
        6  | r2  |  c3    |  3
    
    函数:此函数用于将数组2转换为表

    function convetTable($array) {
          $rows = array(); 
          $rows[0] = array(); 
          foreach ($array as $column) { 
            if (!in_array($column[1],$rows[0])) {
                $rows[0][] = $column[1]; 
            }
          }
          $count = count($rows[0]);
          foreach ($array as $row) {
            $rowTtl = $row[0];
            $rowQty = $row[1];
            $rowVal = $row[2];
            if (!isset($rows[$rowTtl])) $rows[$rowTtl] = array_pad(array(),$count,0);
                    $rows[$rowTtl][array_search($rowQty,$rows[0])] = $rowVal;
           }
        return $rows;
     }
    
    $table = convetTable($array2);
    
    HTML :
    
    {foreach from=$table item=key key=foo}
                            <tr class="prototype" id="first_row">
                            <td><a href="javascript:void(0);" class="remove">Remove Row</a>
                            <td><input type="text" name="rows[]" value="{$foo}" class="number" /></td>
                            {foreach from=$key item=cell}
                                <td align="center"><input type="text" name="cols[]" value="{$cell}" class="id number" /></td>
                             {/foreach}
                            </tr>
                       {/foreach}
    
    注:这里c1、c2、c3、1、2、3是$cell值

    我试图显示$row、$col、$name,但这里只显示$row和$cell

    我需要对应行和列的另一个值名。

    试试这个

    $count = count($array); //this will give you the count of elements of your array...
    echo "<table border="1"><tr><th>ID</th><th>ROWS</th><th>COLUMNS</th><th>NAMES</th></tr>";
    for($i=1;$i<=$count;$i++) //loop through the array..
    {
        echo "<tr><td>$i</td><td>".$array[$i][0]."</td><td>".$array[$i][1]."</td><td>".$array[$i][2]."</td></tr>";
    }
    
    echo "</table>";
    
    $count=count($array)//这将为您提供数组的元素计数。。。
    回音“IdrowsCames”;
    
    对于($i=1;$i仅将数组放入表中的代码就太大了?您需要进一步解释。数组中的
    值在哪里?当前函数的作用是什么?表需要如何显示的示例?请注意,仅转储代码并让我们检查代码以了解其作用不是很有帮助。@i可以让eezburger@NishantSolanki我已经编辑了我的确切问题