PHP 解析多维数组

我有以下 PHP 数组输出:


Array

(

    [ON] => Array

        (

            [0] => Array

                (

                    [Name] => James Smith

                    [Claim/Policy Number] => 787878

                    [Province] => ON

                    [Internal code] => 6987

                    [Total (including tax)] => $ 5,299.97

                )


            [1] => Array

                (

                    [Name] => John Smith

                    [Claim/Policy Number] => 9999999

                    [Province] => ON

                    [Internal code] => 1234

                    [Total (including tax)] => $ 3,136.32

                )


            [2] => Array

                (

                    [Name] => John Smith1

                    [Claim/Policy Number] => 909090

                    [Province] => ON

                    [Internal code] => 2873

                    [Total (including tax)] => $ 1,996.81

                )

        )


    [AB] => Array

        (

            [4] => Array

                (

                    [Name] => John Smith

                    [Claim/Policy Number] => 545454

                    [Province] => AB

                    [Internal code] => 8909

                    [Total (including tax)] => $ 3,720.40

                )


            [7] => Array

                (

                    [Name] => John Smith1

                    [Claim/Policy Number] => 223343

                    [Province] => AB

                    [Internal code] => 1113

                    [Total (including tax)] => $ 3,567.89

                )


        )


)

我正在尝试找到将其格式化为下表结构的最佳方法:


<h2>AB</h2>

<table>

    <tr>

        <td>

        Name

        </td>

        <td>

        Claim/Policy Number

        </td>

        <td>

        Internal Code

        </td>

        <td>

        Total (including tax)

        </td>

    </tr>


我需要循环足够灵活,以便为每个子数组编写一个带有标题的新表。


循环此数组以输出数组中每个子数组的新表和标头的最佳方法是什么?


守候你守候我
浏览 152回答 1
1回答

HUH函数

您只需要在 foreach 中包含一个 foreach 即可。工作示例:<?php$tables = [&nbsp; &nbsp; 'ON' => [&nbsp; &nbsp; &nbsp; &nbsp; '0' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Name' => 'James Smith',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Claim/Policy Number' => '787878',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Province' => 'ON',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Internal code' => '6987',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Total (including tax)' => '$ 5,299.97',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; '1' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Name' => 'John Smith',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Claim/Policy Number' => '9999999',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Province' => 'ON',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Internal code' => '1234',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Total (including tax)' => '$ 3,136.32',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; '2' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Name' => 'John Smith1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Claim/Policy Number' => '909090',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Province' => 'ON',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Internal code' => '2873',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Total (including tax)' => '$ 1,996.81',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; ],&nbsp; &nbsp; 'AB' => [&nbsp; &nbsp; &nbsp; &nbsp; '4' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Name' => 'John Smith',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Claim/Policy Number' => '545454',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Province' => 'AB',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Internal code' => '8909',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Total (including tax)' => '$ 3,720.40',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; '7' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Name' => 'John Smith1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Claim/Policy Number' => '223343',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Province' => 'AB',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Internal code' => '1113',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Total (including tax)' => '$ 3,567.89',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; ]];?><!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Document</title></head><body>&nbsp; &nbsp; <?php foreach($tables as $name => $table): ?>&nbsp; &nbsp; <h2><?= $name ?></h2>&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Claim/Policy Number</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Internal Code</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Total (including tax)</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($table as $row): ?>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?= $row['Name'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?= $row['Claim/Policy Number'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?= $row['Internal code'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?= $row['Total (including tax)'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp; </table>&nbsp; &nbsp; <?php endforeach; ?></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP