在HTML表中显示嵌套数组

时间:2021-02-14 14:32:33

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.

这是一个简单的问题,我一直试图解决几个小时。我有一个数组包含几个学生的信息和他们在测试中得分的标记。总共有8个科目,每个科目有5个参数。

The array looks like below:

该数组如下所示:

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
        )

)

I need to display the following in a nicely formatted HTML table (I'm planning to use Bootstrap), but I can't for the life of me figure out how to properly nest the table using PHP.

我需要在一个格式良好的HTML表格中显示以下内容(我打算使用Bootstrap),但我不能在我的生活中弄清楚如何使用PHP正确地嵌套表格。

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

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

This gives the following result:

这给出了以下结果:

在HTML表中显示嵌套数组

Which is okay.

哪个没关系。

The Actual Problem

实际问题

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.

我正在使用PHP来显示数组中的内容。我很困惑如何显示嵌套的和部分的值。

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).

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

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

一个例子将非常感激,因为我花了无数个小时试图让这个工作,但失败的非常糟糕。

3 个解决方案

#1


1  

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>

#2


3  

Not quite the same output, but here's a recursive approach that should handle any depth of nested arrays...

不完全相同的输出,但这是一个递归方法,应该处理任何深度的嵌套数组...

<?php

$data = Array (
    "Ron" => Array (
            "subject1" => Array (
               "tests" => 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 (
               "tests" => 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 (  
               "tests" => 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 (
                "tests" => Array (
                    "test1" => 39,
                    "test2" => 75,
                    "total" => 114,
                    "percentage" => 76,
                    "status" => "Pass",

                ),
                "pass_count" => 8,
                "fail_count" => 0,
                "gross_total" => 846,
                "gross_percentage" => 70.5,
            ),
        ),
);


print_r($data);
$table = table_cell($data);
echo $table;


function table_cell($data) {

  $return = "<table border='1'>";
  foreach ($data as $key => $value) {
    $return .= "<tr><td>$key</td><td>";
    if (is_array($value)) {
        $return .= table_cell($value);
    } else {
        $return .= $value;
    }
    $return .= "</td><tr>";
  }
  $return .= "</tr></table>";
  return($return);

}

and the table looks nothing like the requested but... it was an interesting excersize...

而且这张桌子看起来并不像要求的那样...但这是一个有趣的例外...

在HTML表中显示嵌套数组

#3


2  

Try this out

试试吧

<table border="1">
    <thead>
    <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>

        </tr>
        <?php
        $numberOfSubjects = 3; //I used 3 subjects. You should change this to 8 for your data.
        foreach ($data as $student => $info) {
            echo "<tr><td rowspan=$numberOfSubjects />$student</td>";

            //flag to let the inner loop the tr has been drawn for the first row
            $firstRow = true;
            foreach ($info as $key => $value) {

                //we only want subject info
                if (strpos($key, "subject") === 0) {
                    if (!$firstRow) {
                        echo "<tr>";
                    } //else we already drew it

                    //its a subject so
                    echo "<td>$key</td>";
                    echo "<td>{$value['test1']}</td>";
                    echo "<td>{$value['test2']}</td>";
                    echo "<td>{$value['total']}</td>";
                    echo "<td>{$value['status']}</td>";
                    echo "<td>{$value['percentage']}</td>";

                    //only draw total for the first row
                    if ($firstRow) {
                        echo "<td rowspan=$numberOfSubjects>{$info['pass_count']}</td>";
                        echo "<td rowspan=$numberOfSubjects>{$info['gross_percentage']}</td>";
                    }
                    //close the row
                    echo "</tr>";
                    $firstRow = false;
                }
            }
        }
        ?>
    </tbody>
</table>

Here is the output:

这是输出:

在HTML表中显示嵌套数组

Its based on a sample dataset I constructed from your description, included below for completeness:

它基于我根据您的描述构建的样本数据集,包括下面的完整性:

<?php
$data = 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,
        "subject2" => Array
            (
            "test1" => 47
            , "test2" => 86
            , "total" => 133
            , "percentage" => 88.67
            , "status" => Pass
        )
        , "subject3" => 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" => 47
            , "test2" => 86
            , "total" => 133
            , "percentage" => 88.67
            , "status" => Pass
        )
        , "pass_count" => 8
        , "fail_count" => 0
        , "gross_total" => 963
        , "gross_percentage" => 80.25,
        "subject2" => Array
            (
            "test1" => 47
            , "test2" => 86
            , "total" => 133
            , "percentage" => 88.67
            , "status" => Pass
        )
        , "subject3" => Array
            (
            "test1" => 48
            , "test2" => 86
            , "total" => 134
            , "percentage" => 89.33
            , "status" => Pass
        )
        , "pass_count" => 8
        , "fail_count" => 0
        , "gross_total" => 963
        , "gross_percentage" => 80.25
    )
);

#1


1  

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>

#2


3  

Not quite the same output, but here's a recursive approach that should handle any depth of nested arrays...

不完全相同的输出,但这是一个递归方法,应该处理任何深度的嵌套数组...

<?php

$data = Array (
    "Ron" => Array (
            "subject1" => Array (
               "tests" => 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 (
               "tests" => 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 (  
               "tests" => 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 (
                "tests" => Array (
                    "test1" => 39,
                    "test2" => 75,
                    "total" => 114,
                    "percentage" => 76,
                    "status" => "Pass",

                ),
                "pass_count" => 8,
                "fail_count" => 0,
                "gross_total" => 846,
                "gross_percentage" => 70.5,
            ),
        ),
);


print_r($data);
$table = table_cell($data);
echo $table;


function table_cell($data) {

  $return = "<table border='1'>";
  foreach ($data as $key => $value) {
    $return .= "<tr><td>$key</td><td>";
    if (is_array($value)) {
        $return .= table_cell($value);
    } else {
        $return .= $value;
    }
    $return .= "</td><tr>";
  }
  $return .= "</tr></table>";
  return($return);

}

and the table looks nothing like the requested but... it was an interesting excersize...

而且这张桌子看起来并不像要求的那样...但这是一个有趣的例外...

在HTML表中显示嵌套数组

#3


2  

Try this out

试试吧

<table border="1">
    <thead>
    <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>

        </tr>
        <?php
        $numberOfSubjects = 3; //I used 3 subjects. You should change this to 8 for your data.
        foreach ($data as $student => $info) {
            echo "<tr><td rowspan=$numberOfSubjects />$student</td>";

            //flag to let the inner loop the tr has been drawn for the first row
            $firstRow = true;
            foreach ($info as $key => $value) {

                //we only want subject info
                if (strpos($key, "subject") === 0) {
                    if (!$firstRow) {
                        echo "<tr>";
                    } //else we already drew it

                    //its a subject so
                    echo "<td>$key</td>";
                    echo "<td>{$value['test1']}</td>";
                    echo "<td>{$value['test2']}</td>";
                    echo "<td>{$value['total']}</td>";
                    echo "<td>{$value['status']}</td>";
                    echo "<td>{$value['percentage']}</td>";

                    //only draw total for the first row
                    if ($firstRow) {
                        echo "<td rowspan=$numberOfSubjects>{$info['pass_count']}</td>";
                        echo "<td rowspan=$numberOfSubjects>{$info['gross_percentage']}</td>";
                    }
                    //close the row
                    echo "</tr>";
                    $firstRow = false;
                }
            }
        }
        ?>
    </tbody>
</table>

Here is the output:

这是输出:

在HTML表中显示嵌套数组

Its based on a sample dataset I constructed from your description, included below for completeness:

它基于我根据您的描述构建的样本数据集,包括下面的完整性:

<?php
$data = 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,
        "subject2" => Array
            (
            "test1" => 47
            , "test2" => 86
            , "total" => 133
            , "percentage" => 88.67
            , "status" => Pass
        )
        , "subject3" => 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" => 47
            , "test2" => 86
            , "total" => 133
            , "percentage" => 88.67
            , "status" => Pass
        )
        , "pass_count" => 8
        , "fail_count" => 0
        , "gross_total" => 963
        , "gross_percentage" => 80.25,
        "subject2" => Array
            (
            "test1" => 47
            , "test2" => 86
            , "total" => 133
            , "percentage" => 88.67
            , "status" => Pass
        )
        , "subject3" => Array
            (
            "test1" => 48
            , "test2" => 86
            , "total" => 134
            , "percentage" => 89.33
            , "status" => Pass
        )
        , "pass_count" => 8
        , "fail_count" => 0
        , "gross_total" => 963
        , "gross_percentage" => 80.25
    )
);