[英]PHP: build html table with rowspans from associative array
I have the following array我有以下数组
Array
(
[Germany] => Array
(
[Channels] => Array
(
[0] => Google Ads
[1] => LinkedIn Ads
[2] => E-Mail-Marketing
)
[Tools] => Array
(
[0] => CRM
)
)
[USA] => Array
(
[SalesTools] => Array
(
[0] => Pipedrive
[1] => Salesforce
)
)
)
and want to show it in a table like this:并希望在这样的表格中显示它:
<table>
<tr>
<th>Region</th>
<th>Costcenter</th>
<th>Costcenter_item</th>
</tr>
<tr>
<td rowspan='4'>Germany</td>
<td rowspan='3'>Channels</td>
<td>Google Ads</td>
</tr>
<tr>
<td>Linkedin</td>
</tr>
<tr>
<td>Email Marketing</td>
</tr>
<tr>
<td rowspan='1' >Tools</td>
<td>CRM</td>
</tr>
<tr>
<td rowspan='2'>USA</td>
<td rowspan='2'>Sales Tools</td>
<td>Pipedrive</td>
</tr>
<tr>
<td>Salesforce</td>
</tr>
</table>
But I get absolutely confused about the rowspans.但是我对行跨度感到非常困惑。 I tried using backtracking on the array to count the "leaves" but I couldnt make it work.我尝试在数组上使用回溯来计算“叶子”,但我无法让它工作。 How can I get the correct rowspan number while building this html table programmatically with php?如何在使用 php 以编程方式构建此 html 表时获得正确的行跨度数?
td
rows and 1 st index being no.所以在下面的代码片段中,我们为每个递归调用返回子结果,这是一个数组,第 0个索引是td
行,第 1个索引是 no。 of leaves encountered so far.到目前为止遇到的叶子。Snippet:片段:
<?php
function getAllCells($d){
$res = [];
$leaves = 0;
foreach($d as $key => $value){
if(is_array($value)){
$sub_res = getAllCells($value);
$res = array_merge($res, [["<td rowspan='".$sub_res[1]."'>$key</td>", $sub_res[1]]], $sub_res[0]);
$leaves += $sub_res[1];
}else{
$res = array_merge($res, [["<td>$value</td>", 0]]);
$leaves++;
}
}
return [$res, $leaves];
}
Once done, we can build up the table again recursively using the above precomputed td
s'.完成后,我们可以使用上述预先计算的td
s' 再次递归地构建表。 The main catch here is to understand when to open and close a tr
.这里的主要问题是了解何时打开和关闭tr
。
We only close a tr
tag if it is the first leaf child.如果它是第一个叶子孩子,我们只关闭一个tr
标签。 Every other leaf child will reside in it's own tr
tags.每个其他叶子孩子都将驻留在它自己的tr
标签中。
Snippet:片段:
<?php
function buildTable($d, $tds, &$ptr, &$table){
$first_leaf_consumed = false;
if($ptr == 0) $table .= "<tr>";
foreach($d as $key => $value){
if(is_array($value)){
$table .= $tds[ $ptr++ ][0];
$sub_res = buildTable($value, $tds, $ptr, $table);
}else{
if(!$first_leaf_consumed){
$table .= $tds[$ptr++][0] . "</tr>";
$first_kid_consumed = true;
}else{
$table .= "<tr>". $tds[$ptr++][0] . "</tr>";
}
}
}
}
Full Code:完整代码:
<?php
$d = Array(
'Germany' => Array
(
'Channels' => Array
(
'0' => 'Google Ads',
'1' => 'LinkedIn Ads',
'2' => 'E-Mail-Marketing'
),
'Tools' => Array
(
'0' => 'CRM'
),
),
'USA' => Array
(
'SalesTools' => Array
(
'0' => 'Pipedrive',
'1' => 'Salesforce'
)
),
);
$tds = getAllCells($d)[0];
$table = <<<EOD
<table border='3'>
<tr>
<th>Region</th>
<th>Costcenter</th>
<th>Costcenter_item</th>
</tr>
EOD;
$ptr = 0;
buildTable($d, $tds, $ptr, $table);
function buildTable($d, $tds, &$ptr, &$table){
$first_leaf_consumed = false;
if($ptr == 0) $table .= "<tr>";
foreach($d as $key => $value){
if(is_array($value)){
$table .= $tds[ $ptr++ ][0];
$sub_res = buildTable($value, $tds, $ptr, $table);
}else{
if(!$first_leaf_consumed){
$table .= $tds[$ptr++][0] . "</tr>";
$first_kid_consumed = true;
}else{
$table .= "<tr>". $tds[$ptr++][0] . "</tr>";
}
}
}
}
$table .= "</table>";
echo $table;
function getAllCells($d){
$res = [];
$leaves = 0;
foreach($d as $key => $value){
if(is_array($value)){
$sub_res = getAllCells($value);
$res = array_merge($res, [["<td rowspan='".$sub_res[1]."'>$key</td>", $sub_res[1]]], $sub_res[0]);
$leaves += $sub_res[1];
}else{
$res = array_merge($res, [["<td>$value</td>", 0]]);
$leaves++;
}
}
return [$res, $leaves];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.