Php 解析多维数组
我有以下PHP数组输出:Php 解析多维数组,php,arrays,loops,multidimensional-array,Php,Arrays,Loops,Multidimensional Array,我有以下PHP数组输出: Array ( [ON] => Array ( [0] => Array ( [Name] => James Smith [Claim/Policy Number] => 787878 [Province] => ON
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>
<!-- Repeat for all [AB] rows -->
<tr>
<td>
[0] Name
</td>
<td>
[0] Claim/Policy Number
</td>
<td>
[0] Internal Code
</td>
<td>
[0] Total (including tax)
</td>
</tr>
<!-- Repeat for all [AB] rows -->
</table>
<h2>ON</h2>
<table>
<tr>
<td>
...
AB
名称
索赔/保单编号
内部代码
总计(含税)
[0]名称
[0]索赔/保单编号
[0]内部代码
[0]总计(含税)
在…上
...
我需要循环足够灵活,以便为每个子数组编写一个带有标题的新表
通过此数组循环为数组中的每个子数组输出新表和头的最佳方法是什么?您只需要在foreach中使用foreach 工作示例:
<?php
$tables = [
'ON' => [
'0' => [
'Name' => 'James Smith',
'Claim/Policy Number' => '787878',
'Province' => 'ON',
'Internal code' => '6987',
'Total (including tax)' => '$ 5,299.97',
],
'1' => [
'Name' => 'John Smith',
'Claim/Policy Number' => '9999999',
'Province' => 'ON',
'Internal code' => '1234',
'Total (including tax)' => '$ 3,136.32',
],
'2' => [
'Name' => 'John Smith1',
'Claim/Policy Number' => '909090',
'Province' => 'ON',
'Internal code' => '2873',
'Total (including tax)' => '$ 1,996.81',
],
],
'AB' => [
'4' => [
'Name' => 'John Smith',
'Claim/Policy Number' => '545454',
'Province' => 'AB',
'Internal code' => '8909',
'Total (including tax)' => '$ 3,720.40',
],
'7' => [
'Name' => 'John Smith1',
'Claim/Policy Number' => '223343',
'Province' => 'AB',
'Internal code' => '1113',
'Total (including tax)' => '$ 3,567.89',
],
]
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php foreach($tables as $name => $table): ?>
<h2><?= $name ?></h2>
<table>
<tr>
<th>Name</th>
<th>Claim/Policy Number</th>
<th>Internal Code</th>
<th>Total (including tax)</th>
</tr>
<?php foreach($table as $row): ?>
<tr>
<td><?= $row['Name'] ?></td>
<td><?= $row['Claim/Policy Number'] ?></td>
<td><?= $row['Internal code'] ?></td>
<td><?= $row['Total (including tax)'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
</body>
</html>
文件
名称
索赔/保单编号
内部代码
总计(含税)
在这种情况下,您需要一种递归方法。脚本非常有效。非常感谢你的帮助!