Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Html Table - Fatal编程技术网

Php 从数组更改表布局

Php 从数组更改表布局,php,arrays,html-table,Php,Arrays,Html Table,我有以下数组: $table = array( 1 => array('2.1'=>3, '2.2'=>3, '2.3'=>3), 2 => array('2.1'=>1, '2.2'=>1, '2.3'=>1), 3 => array('2.1'=>2, '2.2'=>2, '2.3'=>2), 4 => array('2.1'=>2, '2.2'=>2, '2.3'=&g

我有以下数组:

$table = array(
    1 => array('2.1'=>3, '2.2'=>3, '2.3'=>3),
    2 => array('2.1'=>1, '2.2'=>1, '2.3'=>1),
    3 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    4 => array('2.1'=>2, '2.2'=>2, '2.3'=>1),
    5 => null,
    6 => null,
    7 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    8 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    9 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    10 => array('2.1'=>1, '2.2'=>1, '2.3'=>1),
    11 => array('2.1'=>3, '2.2'=>1, '2.3'=>1),
    12 => array('2.1'=>1),
    13 => array('2.1'=>5, '2.2'=>3, '2.3'=>2)
);
我正在复制这张桌子

Month   clicks  clicks2  clicks3
8       2       2        2
9       2       2        2
10      1       1        1
11      3       1        1
12      1               
13      5       3        2
它显示每个月点击三个复选框。 我需要像这样展示桌子

Month    8     9     10     11     12     13
Click    2     2      1      3      1      5
Clicks2  2     2      1      1             3
Clicks3  2     2      1      1             2
我确实有

$period =13; //last month

echo '<table><thead>';
echo '<tr><th>8</th><th>9</th><th>10</th><th>11</th><th>13</th></tr>';
echo '</thead>';
for ($i=$period-5; $i <= $period ;$i++){
    echo '<tr><th>'.$i.'</th>';
    foreach($table[$i] as $checkbox => $val )
    {
        echo '<td>'.$val.'</td>';   
    }
    echo '</tr>';
}
echo '</table>';
$period=13//上个月
回声';
回音“89101113”;
回声';
对于($i=$period-5;$i$val)
{
回显“.$val.”;
}
回声';
}
回声';
我一整天都在苦苦挣扎,没有眼神,这很可能是一个简单的解决办法。有人看到了吗?

这对我很有用:

<?
$table = array(
    1 => array('2.1'=>3, '2.2'=>3, '2.3'=>3),
    2 => array('2.1'=>1, '2.2'=>1, '2.3'=>1),
    3 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    4 => array('2.1'=>2, '2.2'=>2, '2.3'=>1),
    5 => null,
    6 => null,
    7 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    8 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    9 => array('2.1'=>2, '2.2'=>2, '2.3'=>2),
    10 => array('2.1'=>1, '2.2'=>1, '2.3'=>1),
    11 => array('2.1'=>3, '2.2'=>1, '2.3'=>1),
    12 => array('2.1'=>1),
    13 => array('2.1'=>5, '2.2'=>3, '2.3'=>2)
);

$checkboxes = array();
foreach($table as $monthtable) {
    foreach($monthtable as $checkbox=>$clicks) {
        if(in_array($checkbox, $checkboxes)) {
            continue;
        }
        else {
            $checkboxes[] = $checkbox;
        }
    }
}

// now we have $checkboxes containing 2.1, 2.2, 2.3, our rows.
?>
<table>
    <tr>
        <th>Month</th>
        <? foreach($table as $month=>$monthtable) {
            echo '<th>'.$month.'</th>';
        } ?>
    </tr>
    <? foreach($checkboxes as $checkbox) {
        echo '<tr>'
            . '<th>'.$checkbox.'</th>';

        // assuming this is still in the same order as the last loop.
        foreach($table as $monthtable) {
            echo '<td>';
            if(array_key_exists($checkbox, $monthtable)) {
                echo $monthtable[$checkbox];
            }
            else {
                echo '0';
            }
            echo '</td>';
        }

        echo '</tr>';
    } ?>
</table>

它工作得很好!!!我试过了。现在我将研究代码,看看我遗漏了什么。真的谢谢克雷格