如何在html表中显示PHP多维关联数组? [英] How to display the PHP multiple dimensional associative array in html table?

查看:203
本文介绍了如何在html表中显示PHP多维关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的多维关联数组.

Below is my multiple dimensional associative array.

   <?php

$city = array(
    "schoolDetails" => array(
        "schoolOne" => array(
            "schoolName" => "TCS",
            "branchName" => "Iqbal Campus",
            "phone"      => "4256791",
            "numberOfTeachers" => 50,
            "departments" => array(
                "Maths",
                "English",
                "Science",
                "Computer"
            ),
            "teachers" => array(
                "Mr Ali",
                "Mr Waseem",
                "Mr Kashif",
                "Mr Adeel"
            )
        ),

        "schoolTwo" => array(
            "schoolName" => "CSS",
            "branchName" => "Gohdpur Campus",
            "phone"      => "4261201",
            "numberOfTeachers" => 30,
            "departments" => array(
                "Islamiyat",
                "Pakistan Studies",
                "Urdu",
                "Arts"
            ),
            "teachers" => array(
                "Mr Naeem",
                "Mr Waris",
                "Mr Kamran",
                "Mr Aleem"
            )
        )
    )
);
?>

我对如何以html表格形式显示结果感到困惑.以及如何回显代码. 我尝试输出结果的方式是

I am confused about how to display the result in tabular form in html. As well as how to echo the code. The way I tried to output the result is

    foreach ($city as $school ) {
    # code...
    foreach ($school as $schools ) {
        # code...
        foreach ($schools as $key => $value) {
            # code...
            echo $key . " = " . $value . "<br>";
        }
    }
}

但是在此不打印我的部门和教师索引.那么如何访问它们并打印它们.

But in this my department and teachers index are not printed. So how to access them and print them.

推荐答案

  • 首先成为头条新闻
  • 然后遍历学校
  • 输出列及其内容
  • echo "<table>\n";
    echo "<tr>";
    foreach (array_keys(reset($city['schoolDetails'])) as $headline) {
        echo "<th>$headline</th>";
    }
    echo "</tr>\n";
    
    foreach ($city['schoolDetails'] as $school) {
        echo "<tr>";
        displayColumn($school);
        echo "</tr>\n";
    }
    echo "</table>\n";
    
    function displayColumn(array $array)
    {
        foreach ($array as $key => $value) {
            echo "<td>";
            if (is_array($value)) {
                echo implode("<br>\n", $value);
            } else {
                echo $value;
            }
            echo "</td>";
        }
    }
    

    这篇关于如何在html表中显示PHP多维关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆