Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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多维数组在html表视图中打印_Php - Fatal编程技术网

php多维数组在html表视图中打印

php多维数组在html表视图中打印,php,Php,我有这个阵列: Array ( [Zahid] => Array ( [physics] => 35 [maths] => 30 [chemistry] => 39 ) [Amir] => Array ( [physics] => 30 [maths] => 32

我有这个阵列:

Array
(
    [Zahid] => Array
        (
            [physics] => 35
            [maths] => 30
            [chemistry] => 39
        )

    [Amir] => Array
        (
            [physics] => 30
            [maths] => 32
            [chemistry] => 29
        )

    [Kundan] => Array
        (
            [physics] => 31
            [maths] => 22
            [chemistry] => 39
        )

    [Narayan] => Array
        (
            [physics] => 31
            [maths] => 22
            [chemistry] => 39
        )

)
我想用这个HTML表格格式显示给定的数组。我尝试使用foreach循环,但它没有显示正确的格式。我怎么做

<table border="1">
    <tr>
        <th>Zahid</th>
        <th>Amir</th>
        <th>Kundan</th>
        <th>Narayan</th>
    </tr>

    <tr>
        <td>physics : 35</td>
        <td>physics : 30</td>
        <td>physics : 31</td>
        <td>physics : 31</td>
    </tr>

    <tr>
        <td>maths : 30</td>
        <td>maths : 32</td>
        <td>maths : 22</td>
        <td>maths : 22</td>

    </tr>

    <tr>
        <td>chemistry : 39</td>
        <td>chemistry : 29</td>
        <td>chemistry : 39</td>
        <td>chemistry : 39</td>
    </tr>

</table>

扎希德
阿米尔
昆丹
那拉扬
物理:35
物理:30
物理:31
物理:31
数学:30
数学:32
数学:22
数学:22
化学:39
化学:29
化学:39
化学:39

您将无法在主数组上使用嵌套的foreach循环,因为最终将学生作为行,主题作为列,这与您想要的相反。当你说它没有显示正确的格式时,我想这就是你得到的。您可以从一名学生处获得科目列表:

$subjects = array_keys(reset($students));
然后迭代该行以获得您的行,并在每行中迭代学生列表以获得该行显示的主题的分数

// each subject is a row
foreach ($subjects as $subject) {

    // each student is a column
    foreach ($students as $student => $grades) {
        echo "$subject : $grades[subject]";
    }
}

这是一般的想法,我相信您可以找出表标记,因此为了简单起见,我省略了它。

我不想问您为什么会忽略对学生的引用。。。 因此,最简单的解决方法是:

    // student
    $students = array_keys($array);

    //physics 
    $physics = array_column($array, 'physics');

    //maths
    $maths = array_column($array, 'maths');

最后,对从$array检索到的数据执行任何操作。

“我尝试了foreach循环,但它没有显示coect格式。”显示您尝试的代码和结果输出。如果您还包括数组的var_导出,那就更好了。您可能需要根据主题对数组中的数据重新排序,而不是像数组这样的名称([Physical]=>array([Zahid]=>35[Amir]=>30))安德烈亚斯-我写这个转换器就是为了这样一个东西,它可以将
print\u r
var\u dump
转换为
var\u export
-它工作得很好,不必介意我网站的其他部分,因为我似乎从来没有时间去做它。您可以在上找到它的源代码