Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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/4/macos/9.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_Multidimensional Array_Html Table - Fatal编程技术网

PHP中的多维数组到表

PHP中的多维数组到表,php,arrays,multidimensional-array,html-table,Php,Arrays,Multidimensional Array,Html Table,我希望你能帮我完成作业。我对PHP语言还是新手。我有这样一个案例: <!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; text-alig

我希望你能帮我完成作业。我对PHP语言还是新手。我有这样一个案例:

<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    th,
    td {
      border: 1px solid black;
      border-collapse: collapse;
    }

    th,
    td {
      padding: 5px;
      text-align: left;
    }
  </style>
</head>

<body>

  <?php
    $ub = array(
      "Fakultas Ilmu Komputer" => array(
                                    array(
                                        "Jurusan Teknik Informatika",
                                        array(
                                            "Prodi Teknik Informatika",
                                            "Prodi Teknik Komputer",
                                            "Prodi Magister Ilmu Komputer"
                                        )
                                    ),
                                    array(
                                        "Jurusan Sistem Informasi",
                                        array(
                                            "Prodi Sistem Informasi",
                                            "Prodi Teknologi Informasi",
                                            "Prodi Pendidikan Teknologi Informasi"
                                        )
                                    )
                                ),
    "Fakultas Kedokteran" => array(
                                    array(
                                        "Jurusan Kedokteran",
                                        array(
                                            "Prodi Pendidikan Dokter"
                                        )
                                    ),
                                    array(
                                        "Jurusan Keperawatan dan Gizi",
                                        array(
                                            "Prodi Ilmu Keperawatan",
                                            "Prodi Ilmu Gizi",
                                            "Prodi Farmasi",
                                            "Prodi Kebidanan"
                                        )
                                    )
                                )
  );

    ?>

    <table>
      <tr>
        <th>Fakultas</th>
        <th>Jurusan</th>
        <th>Program Studi</th>
      </tr>

<?php
foreach($ub as $fakultas => $jurusan) {
    $fakultasLine = true;

    // calculate inside rows per row
    $insideRows = 0;
    foreach($jurusan as $program) {
        foreach($program[1] as $studi) {
            $insideRows++;
        }
    }
    foreach($jurusan as $program) {
        $jurusanLine = true;
        foreach($program[1] as $studi) {
            echo '<tr>';
            if($fakultasLine) {
                echo '<td rowspan="' . $insideRows . '">' . $fakultas . '</td>';
                $fakultasLine = false;
            }
            if($jurusanLine) {
                echo '<td rowspan="' . count($program[1]) . '">' . $program[0] . '</td>';
                $jurusanLine = false;
            }
            echo '<td>' . $studi . '</td>';
            echo '</tr>';
        }
    }
}
?>


    </table>
</body>

</html>

表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
th,td{
填充物:5px;
文本对齐:左对齐;
}
蚕豆
朱鲁桑
程序研究

您应该稍微更改数组,使所有列位于同一级别,如下所示:

  <?php
    $ub = array(
      "Fakultas Ilmu Komputer" => array(
                                    array(
                                        "Jurusan Teknik Informatika",
                                        array(
                                            "Prodi Teknik Informatika",
                                            "Prodi Teknik Komputer",
                                            "Prodi Magister Ilmu Komputer"
                                        )
                                    ),
                                    array(
                                        "Jurusan Sistem Informasi",
                                        array(
                                            "Prodi Sistem Informasi",
                                            "Prodi Teknologi Informasi",
                                            "Prodi Pendidikan Teknologi Informasi"
                                        )
                                    )
                                ),
    "Fakultas Kedokteran" => array(
                                    array(
                                        "Jurusan Kedokteran",
                                        array(
                                            "Prodi Pendidikan Dokter"
                                        )
                                    ),
                                    array(
                                        "Jurusan Keperawatan dan Gizi",
                                        array(
                                            "Prodi Ilmu Keperawatan",
                                            "Prodi Ilmu Gizi",
                                            "Prodi Farmasi",
                                            "Prodi Kebidanan"
                                        )
                                    )
                                )
  );

    ?>

之后,您必须跟踪表的所有“内部行”,并打印嵌套在最深循环中的行。像这样:

<?php
foreach($ub as $fakultas => $jurusan) {
    $fakultasLine = true;

    // calculate inside rows per row
    $insideRows = 0;
    foreach($jurusan as $program) {
        foreach($program[1] as $studi) {
            $insideRows++;
        }
    }
    foreach($jurusan as $program) {
        $jurusanLine = true;
        foreach($program[1] as $studi) {
            echo '<tr>';
            if($fakultasLine) {
                echo '<td rowspan="' . $insideRows . '">' . $fakultas . '</td>';
                $fakultasLine = false;
            }
            if($jurusanLine) {
                echo '<td rowspan="' . count($program[1]) . '">' . $program[0] . '</td>';
                $jurusanLine = false;
            }
            echo '<td>' . $studi . '</td>';
            echo '</tr>';
        }
    }
}
?>

您的完整代码片段看起来有点像这样:

<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    th,
    td {
      border: 1px solid black;
      border-collapse: collapse;
    }

    th,
    td {
      padding: 5px;
      text-align: left;
    }
  </style>
</head>

<body>

  <?php
    $ub = array(
      "Fakultas Ilmu Komputer" => array(
                                    array(
                                        "Jurusan Teknik Informatika",
                                        array(
                                            "Prodi Teknik Informatika",
                                            "Prodi Teknik Komputer",
                                            "Prodi Magister Ilmu Komputer"
                                        )
                                    ),
                                    array(
                                        "Jurusan Sistem Informasi",
                                        array(
                                            "Prodi Sistem Informasi",
                                            "Prodi Teknologi Informasi",
                                            "Prodi Pendidikan Teknologi Informasi"
                                        )
                                    )
                                ),
    "Fakultas Kedokteran" => array(
                                    array(
                                        "Jurusan Kedokteran",
                                        array(
                                            "Prodi Pendidikan Dokter"
                                        )
                                    ),
                                    array(
                                        "Jurusan Keperawatan dan Gizi",
                                        array(
                                            "Prodi Ilmu Keperawatan",
                                            "Prodi Ilmu Gizi",
                                            "Prodi Farmasi",
                                            "Prodi Kebidanan"
                                        )
                                    )
                                )
  );

    ?>

    <table>
      <tr>
        <th>Fakultas</th>
        <th>Jurusan</th>
        <th>Program Studi</th>
      </tr>

<?php
foreach($ub as $fakultas => $jurusan) {
    $fakultasLine = true;

    // calculate inside rows per row
    $insideRows = 0;
    foreach($jurusan as $program) {
        foreach($program[1] as $studi) {
            $insideRows++;
        }
    }
    foreach($jurusan as $program) {
        $jurusanLine = true;
        foreach($program[1] as $studi) {
            echo '<tr>';
            if($fakultasLine) {
                echo '<td rowspan="' . $insideRows . '">' . $fakultas . '</td>';
                $fakultasLine = false;
            }
            if($jurusanLine) {
                echo '<td rowspan="' . count($program[1]) . '">' . $program[0] . '</td>';
                $jurusanLine = false;
            }
            echo '<td>' . $studi . '</td>';
            echo '</tr>';
        }
    }
}
?>


    </table>
</body>

</html>

桌子
th,
运输署{
边框:1px纯黑;
边界塌陷:塌陷;
}
th,
运输署{
填充物:5px;
文本对齐:左对齐;
}
蚕豆
朱鲁桑
程序研究
你想要的就是这个


同时让我知道它是否对您有效…

您需要学习
行span
。访问这里,我的意思不仅是行跨度,我在使用我的代码进行循环时遇到了问题。好的,然后等待
“Fakultas Ilmu Komputer”,
应该向下一行,这将正确地将其放入下一个子数组中。请更正您的示例。请使用foreach Loop提供的动态html格式和代码。您展示了一些比我所玩的更好的技术。您可以使用
foreach($jurusan as$program){$insideRows+=count($program[1]);}
删除一个for循环。干得不错。@mickmackusa我知道。。。但这更具可读性。递归方法可以用更少的代码行来打印,但不会有太多解释性。。。