PHP:用关联数组的行跨度构建html表



我有以下数组

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

,并希望在这样的表中显示:

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

但是我对行跨度感到非常困惑。我尝试在数组上使用回溯来计算"叶子"。但我做不到。我怎么能得到正确的行跨度数,而建立这个html表与php编程?

  • 在这里,我们需要得到一个键有多少个叶子,以计算特定td的行跨度。所以在下面的代码片段中,我们为每个递归调用返回子结果,这是一个数组,0th索引是td行,1st索引是no。

片段:

<?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];
}
  • 完成后,我们可以使用上面预先计算的tds'再次递归地构建表。这里的主要问题是了解何时打开和关闭tr

  • 我们只关闭tr标记,如果它是第一个叶子。每个其他的叶子子节点都将驻留在它自己的tr标签中。

片段:

<?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>";
}
}   
}
}

完整代码:

<?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];
}
<<p>

在线演示/strong>在线UI演示

最新更新