-1
您好我正在嘗試打印一個數組,其中包含節點值和深度參數,如下表所示。從數組中打印表格格式的樹
我當前的代碼,它將打印如下樹視圖格式:
if(count($nodes) > 0){
$depth = -1;
$flag = false;
foreach ($nodes as $row) {
while ($row->depth > $depth) {
echo "<ul><li><a href='javascript:void(0);'>";
$flag = false;
$depth++;
}
while ($row->depth < $depth) {
echo "</a></li></ul>";
$depth--;
}
if ($flag) {
echo "</a></li><li><a href='javascript:void(0);'>";
$flag = false;
}
echo $row->name.'<button type="button" class="btn btn-danger margin-left-20 removeBtn" data-name="'.$row->name.'">Delete</button>' ;
$flag = true;
}
while ($depth-- > -1) {
echo "</a></li></ul>";
}
}else{
echo '<div class="alert alert-warning"><strong>Warning!</strong> There were no nodes in the tree!</div>';
}
示例陣列:
Value Depth
12 0
13 1
14 1
25 2
20 2
21 2
16 2
23 3
24 3
我能在一個正常的樹視圖打印,但是當我試圖作爲表格格式打印,我無法以某種方式進行打印。
預期格式:
表第一行12 [0]的整個行
表第二行將由13共享[1]和14 [1]
表第三行將被共享由25 [2],20 [2]和21 [] 2
表第四行將由共享16 [3],23 [3]和24 [3]
所以邏輯的每一行該表將被共享同盟深度節點值。
因此不'我寫代碼爲我requirements'要求的服務,你應該已經知道現在。顯示您當前的代碼 –
我目前的代碼打印爲樹形視圖,但我甚至沒有任何想法,無論如何,我將添加我當前的代碼。 –
我建議你閱讀** [如何提出完美的問題](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)**,下一步** [如何創建最小,完整和可驗證的例子](https://stackoverflow.com/help/mcve)** - 然後編輯您的問題,以潛在吸引更多的關注。 –