PHP数组的表格表

时间:2015-09-15 22:27:52

标签: php html arrays html-table

我遇到了问题,我相信,你可以帮助我。

我有下一个数组:

JSON:

{
"1":{
"01:00":"1",
"02:00":""
},
"2":{
"01:00":"3",
"02:00":""
},
"3":{
"01:00":"23",
"02:00":""
},
"4":{
"01:00":"234",
"02:00":""
},
"5":{
"01:00":"234",
"02:00":""
},
"6":{
"01:00":"",
"02:00":""
},
"7":{
"01:00":"",
"02:00":""
}
}

PHP:

array (
    1 => array ( '01:00' => '1', '02:00' => '', ),
    2 => array ( '01:00' => '3', '02:00' => '', ),
    3 => array ( '01:00' => '23', '02:00' => '', ),
    4 => array ( '01:00' => '234', '02:00' => '', ),
    5 => array ( '01:00' => '234', '02:00' => '', ),
    6 => array ( '01:00' => '', '02:00' => '', ),
    7 => array ( '01:00' => '', '02:00' => '', ),
)

我正在尝试将正确的值放到正确的位置,但我处于死胡同:(

你能帮助我使用这个数组的表生成器。

它应该看起来像输入中的值: enter image description here

第一行是非常静态的:

    echo "<table><tr>";
    for ($i = 0; $i < 8; $i++) {
        echo "<td>$days[$i]</td>";
    }
    echo "</tr>";

然后问题开始了。

3 个答案:

答案 0 :(得分:2)

尝试以下代码

<?php
$days = array (
    1 => array ( '01:00' => '1', '02:00' => '', ),
    2 => array ( '01:00' => '3', '02:00' => '', ),
    3 => array ( '01:00' => '23', '02:00' => '', ),
    4 => array ( '01:00' => '234', '02:00' => '', ),
    5 => array ( '01:00' => '234', '02:00' => '', ),
    6 => array ( '01:00' => '', '02:00' => '', ),
    7 => array ( '01:00' => '', '02:00' => '', ),
);

echo "<table border='1'><thead><th>0</th>";

foreach($days as $k=>$v){   
    echo "<th>".$k."</th>"; 
}
echo "</thead><tbody>";

$rows = array_keys($days[1]);//edited

foreach($rows as $row){
    echo "<tr><td>".$row."</td>";
    foreach($days as $key=>$val){
        echo "<td>";
        echo "<input type='text' name='txt".$key."' value='".$days[$key][$row]."' style='width:40px;' />";//edited
        echo "</td>";
    }
    echo "</tr>";
}

echo "</tbody></table>";


?>

答案 1 :(得分:1)

你可以做的是循环遍历表格的y轴和x轴并从数组中提取值。

$array = array (
    1 => array ( '01:00' => '1', '02:00' => '', ),
    2 => array ( '01:00' => '3', '02:00' => '', ),
    3 => array ( '01:00' => '23', '02:00' => '', ),
    4 => array ( '01:00' => '234', '02:00' => '', ),
    5 => array ( '01:00' => '234', '02:00' => '', ),
    6 => array ( '01:00' => '', '02:00' => '', ),
    7 => array ( '01:00' => '', '02:00' => '', ),
);

echo "<table>\n";
for ($y = 0; $y < count(reset($array)); $y++) {
    echo "\t<tr>\n";
    for ($x = 0; $x <= count($array); $x++) {
        echo "\t\t<td>";
        if ($y == 0) {
            echo $x;
        }
        elseif (isset($array[$x])) {
            echo $array[$x]["0$y:00"];
        }
        else {
            echo "0$y:00";
        }
        echo "</td>\n";
    }
    echo "\t</tr>\n";
}
echo "\n</table>";

这是输出:

<table>
    <tr>
        <td>0</td>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
    </tr>
    <tr>
        <td>01:00</td>
        <td>1</td>
        <td>3</td>
        <td>23</td>
        <td>234</td>
        <td>234</td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>02:00</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>

</table>

答案 2 :(得分:1)

请记住,您已经拥有一系列数组。循环遍历数组时,需要循环遍历内部数组。

就个人而言,我喜欢while循环,如果你不介意反过来,这个例子应该有所帮助。

function makeTableFromArray($array){
    $array = array_values($array);
    $tbl = "<table>";
    $i = count($array);
    while($i--){
        $tbl .= "<tr>";
        $row = array_values($array[$i]);
        $n = count($row);
        while($n--){
            $tbl .= "<td>".$row[$n]."</td>";
        }
        $tbl .= "</tr>";
    }
    $tbl .= "</table>";
    return $tbl;
}

以下是一个例子:https://3v4l.org/L5Wfu

编辑如果您不想反过来,可以先反转数组:makeTableFromArray(array_reverse($array));