显示嵌套阵列中的一个HTML表格 [英] Displaying a nested array in an HTML table

查看:181
本文介绍了显示嵌套阵列中的一个HTML表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在努力解决几个小时一个简单的问题。我有一个包含他们在测试中拿下了几个学生和标记信息的数组。有8个学科共,具有5个参数,每个主题。

This is a simple problem that I've been trying to solve for several hours. I have an array containing information of several students and marks they scored in tests. There are 8 subjects in total, with each subject having 5 parameters.

该阵列看起来像如下:

Array
(
    [Ron] => Array
        (
            [subject1] => Array
                (
                    [test1] => 47
                    [test2] => 86
                    [total] => 133
                    [percentage] => 88.67
                    [status] => Pass
                )
            [pass_count] => 8
            [fail_count] => 0
            [gross_total] => 963
            [gross_percentage] => 80.25

            [...]

            [subject8] => Array
                (
                    [test1] => 48
                    [test2] => 86
                    [total] => 134
                    [percentage] => 89.33
                    [status] => Pass
                )

            [pass_count] => 8
            [fail_count] => 0
            [gross_total] => 900
            [gross_percentage] => 75.50
        )

    [John] => Array
        (
            [subject1] => Array
                (
                    [test1] => 39
                    [test2] => 72
                    [total] => 111
                    [percentage] => 74
                    [status] => Pass
                )
            [pass_count] => 8
            [fail_count] => 0
            [gross_total] => 963
            [gross_percentage] => 80.25

            [...]                

            [subject8] => Array
                (
                    [test1] => 39
                    [test2] => 75
                    [total] => 114
                    [percentage] => 76
                    [status] => Pass
                )

            [pass_count] => 8
            [fail_count] => 0
            [gross_total] => 846
            [gross_percentage] => 70.5
        )

)

我需要显示格式良好的HTML表(我打算使用引导程序)以下的,但我不能为我的生活弄清楚如何正确嵌套表的使用PHP

这是我目前拥有的HTML标记: http://jsfiddle.net/9y910uvp/

This is the HTML markup that I currently have: http://jsfiddle.net/9y910uvp/

这给出了以下结果:

这是好的。

的实际问题

我使用PHP来显示阵列的内容。我很困惑如何为显示值内嵌的< TR> < TD> 部分

I'm using PHP to display the contents from the array. I'm confused how to display the values for the nested <tr> and <td> sections.

我如何遍历数组并正确显示的内容? (我会发布我的努力,到目前为止,但他们都是愚蠢的,没有工作,所以我不发布)。

How do I loop through the array and display the contents correctly? (I'd post my attempts so far, but they're all stupid and not working so I'm not posting).

因为我花了无数时间试图得到这个工作,但非常失败的一个例子是大大AP preciated。

An example would be greatly appreciated because I've spent countless hours trying to get this working, but failed terribly.

推荐答案

从你的情况来看阵,这可能是这样的,你在找什么:

Judging from your array, this might be something like what you're looking for:

<table border="1">
    <thead>
        <tr>
            <th>Name</th>
            <th>Subject</th>
            <th>Test1 Marks</th>
            <th>Test2 Marks</th>
            <th>Total Marks</th>
            <th>Status</th>
            <th>Percentage</th>
            <th>Pass Count</th>      
            <th>Total Percentage</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($arr as $name => $subjects): ?>
            <?php $i = 0; ?>
            <?php foreach($subjects as $subjectName => $subject): ?>
                <?php if (is_array($subject)): ?>
                    <tr>
                        <?php if ($i === 0): ?>
                            <td rowspan="8"><?php echo $name; ?></td>
                        <?php endif; ?>
                        <td><?php echo $subjectName; ?></td>
                        <td><?php echo $subject['test1']; ?></td>
                        <td><?php echo $subject['test2']; ?></td>
                        <td><?php echo $subject['total']; ?></td>
                        <td><?php echo $subject['status']; ?></td>
                        <td><?php echo $subject['percentage']; ?></td>     
                        <?php if ($i === 0): ?>
                            <td rowspan="8"><?php echo $subjects['pass_count']; ?></td>
                            <td rowspan="8"><?php echo $subjects['gross_percentage']; ?></td>
                        <?php endif; ?>
                    </tr>
                <?php endif; ?>
                <?php $i++; ?>
            <?php endforeach; ?>
        <?php endforeach; ?>
    </tbody>
</table>

这篇关于显示嵌套阵列中的一个HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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