在php中以ul li格式解析树数据时遇到一些困难

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在php中以ul li格式解析树数据时遇到一些困难相关的知识,希望对你有一定的参考价值。

我试图创建试验平衡我以某种方式在我的阵列中实现树视图,现在我想打印我的代码,但不知道如何制作树,因为我不知道我将来可以得到多少孩子,我'我不是很擅长递归下面是我的树准备数组,我也附上我的结果,我可以通过foreach循环,但卡在儿童节点我可以通过foreach制作儿童节点,但问题是它不是一个最好的方法,所以请帮我按照我的数组打印我的树结构数据。任何帮助,将不胜感激。

这里我的树阵列看起来像:Tree Array

我的结果看起来像这只是一个数组的根节点,我使用jstree来显示层次结构。

enter image description here

<?
        $total_opening_debit = 0;
        $total_opening_credit = 0;
        $total_debit = 0;
        $total_credit = 0; 
        foreach($this->trees as $key => $value) 
        {
            foreach($value as $tree)
            {
                if($tree["id"] != "")
                {
                ?>
                    <li>
                        <span class="first"><? echo $tree["name"]; ?></span>
                        <span class="other">
                        <?
                            if ($tree["opening_debit"] > $tree["opening_credit"])
                            {
                                echo format_currency($tree["opening_debit"] - $tree["opening_credit"], 2) . " Dr.";
                            }
                            elseif ($tree["opening_credit"] > $tree["opening_debit"])
                            {
                                echo format_currency($tree["opening_credit"] - $tree["opening_debit"], 2) . " Cr.";
                            }
                            else
                            {
                                echo "0";
                            }

                            $total_opening_debit += $tree["opening_debit"];
                            $total_opening_credit += $tree["opening_credit"];
                        ?>
                        </span>
                        <span class="other">
                        <?
                            if($tree["period_debit"]<>"" && $tree["period_debit"]<>"0")
                            {
                                echo format_currency($tree["period_debit"], 2);
                                $total_debit += $tree["period_debit"];
                            }
                            else
                            {
                                echo 0;
                            }
                        ?>
                        </span>
                        <span class="other">
                        <? 
                            if($tree["period_credit"]<>"" && $tree["period_credit"]<>"0")
                            {
                                echo format_currency($tree["period_credit"], 2);
                                $total_credit += $tree["period_credit"];
                            }
                            else
                            {
                                echo 0;
                            } 
                        ?>
                        </span>
                        <span class="other">
                        <? 
                            if ($tree["opening_debit"] + $tree["period_debit"] > $tree["opening_credit"] + $tree["period_credit"])
                            {    
                                $closing =  format_currency($tree["opening_debit"] + $tree["period_debit"] - $tree["opening_credit"] - $tree["period_credit"], 2) . " Dr.";
                            }
                            elseif ($tree["opening_credit"] + $tree["period_credit"] > $tree["opening_debit"] + $tree["period_debit"])
                            {             
                                $closing = format_currency($tree["opening_credit"] + $tree["period_credit"] - $tree["opening_debit"] - $tree["period_debit"], 2) . " Cr.";
                            }
                            else
                            {
                                $closing = "0";
                            }
                            echo $closing;

                            //calculate all opeing and closing debit & credit.
                            if ($tree["opening_debit"] + $tree["period_debit"] > $tree["opening_credit"] + $tree["period_credit"])
                            {     
                                $total_closing_debit += ($tree["opening_debit"] + $tree["period_debit"] - $tree["opening_credit"] - $tree["period_credit"]);
                            }

                            if ($tree["opening_credit"] + $tree["period_credit"] > $tree["opening_debit"] + $tree["period_debit"])
                            {
                                $total_closing_credit += ($tree["opening_credit"] + $tree["period_credit"] - $tree["opening_debit"] - $tree["period_debit"]);
                            }
                        ?>
                        </span>
                        <?
                        if(count($tree["children"]) > 0)
                        {
                            //make_children_tree();
                        }
                        ?>
                    </li>
                <?
                }
            }        
        }


    ?>
答案

我在这里解决了我自己选择的方法我将children数组提取到其他数组中并在该数组上使用我的递归来做魔法。

<? $array = $trial_tree["children"]; ?>


$trial_balance_tree = $this->trees;

$total_opening_debit = 0;
$total_opening_credit = 0;
$total_debit = 0;
$total_credit = 0;
$total_closing_debit = 0;
$total_closing_credit = 0;
$total_opening = 0;
$total_closing = 0;

function makeListItems($a) {
    global $total_opening_debit,$total_opening_credit,$total_credit,$total_debit,$total_closing_debit,$total_closing_credit,$total_opening,$total_closing;
    $out = '';
    foreach($a as $val) {
    //print_r($key); echo is_array($val);exit;
        if($val["id"] > 0)
        {
            $out .= '<li>';
            $out .= "<span class='first'>" . $val["name"] . "</span>";
            $out .= "<span class='other'>"; 
                        if ($val["opening_debit"] > $val["opening_credit"])
                        {
                            $out .= format_currency($val["opening_debit"] - $val["opening_credit"], 2) . " Dr.";
                        }
                        elseif ($val["opening_credit"] > $val["opening_debit"])
                        {
                            $out .= format_currency($val["opening_credit"] - $val["opening_debit"], 2) . " Cr.";
                        }
                        else
                        {
                            $out .= "0";
                        }

                        $total_opening_debit += $val["opening_debit"];
                        $total_opening_credit += $val["opening_credit"]; 
             $out .= "</span>";
             $out .= "<span class='other'>";
                        if($val["period_debit"]<>"" && $val["period_debit"]<>"0")
                        {
                            $out .= format_currency($val["period_debit"], 2);
                            $total_debit += $val["period_debit"];
                        }
                        else
                        {
                            $out .= 0;
                        }
             $out .= "</span>";
             $out .= "<span class='other'>";
                        if($val["period_credit"]<>"" && $val["period_credit"]<>"0")
                        {
                            $out .= format_currency($val["period_credit"], 2);
                            $total_credit += $val["period_credit"];
                        }
                        else
                        {
                            $out .= 0;
                        }
             $out .= "</span>";
             $out .= "<span class='other'>";
                        if ($val["opening_debit"] + $val["period_debit"] > $val["opening_credit"] + $val["period_credit"])
                        {    
                            $closing =  format_currency($val["opening_debit"] + $val["period_debit"] - $val["opening_credit"] - $val["period_credit"], 2) . " Dr.";
                        }
                        elseif ($val["opening_credit"] + $val["period_credit"] > $val["opening_debit"] + $val["period_debit"])
                        {             
                            $closing = format_currency($val["opening_credit"] + $val["period_credit"] - $val["opening_debit"] - $val["period_debit"], 2) . " Cr.";
                        }
                        else
                        {
                            $closing = "0";
                        }
                        $out .= $closing;

                        //calculate all opeing and closing debit & credit.
                        if ($val["opening_debit"] + $val["period_debit"] > $val["opening_credit"] + $val["period_credit"])
                        {     
                            $total_closing_debit += ($val["opening_debit"] + $val["period_debit"] - $val["opening_credit"] - $val["period_credit"]);
                        }

                        if ($val["opening_credit"] + $val["period_credit"] > $val["opening_debit"] + $val["period_debit"])
                        {
                            $total_closing_credit += ($val["opening_credit"] + $val["period_credit"] - $val["opening_debit"] - $val["period_debit"]);
                        }
             $out .= "</span>";

            if(array_key_exists('children', $val)) {
                $out .= makeList($val['children']);
            }
            $out .= '</li>';
        }
    }

    return $out;
}

function makeList($a) {
    $out = '<ul>';
    $out .= makeListItems($a);
    $out .= '</ul>';

    return $out;
}
?>
<table style="width:100%;background-color: #A5C6DB;">
    <tr>
        <th class="table_first" style="width:42.5%;">Ledger Name</th>
        <th class="table_other" style="width:14.7%;">Opening</th>
        <th class="table_other" style="width:14.7%;">Debit</th>
        <th class="table_other" style="width:14.7%;">Credit</th>
        <th class="table_other" style="width:15%;">Closing</th>
    </tr>
</table>
<?
echo "<div id='trial_balance_tree'>";
echo makeList($trial_balance_tree);
echo "</div>";

现在我的观点看起来像这样,这就是我想要的。 enter image description here

以上是关于在php中以ul li格式解析树数据时遇到一些困难的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP DOMDocument 解析脏 html 代码时遇到困难

当一些 <ul> 没有 <li> 子元素时,从 <ul> 数组中抓取 <li>

如何在 WSO2 中以 XML 格式转换 JSON 消息

我在HTML中写了一个<ul><li>标签 ,怎麼把数据库的数据动态动态生成到标签里面??php

PHP 格式化列表项

将 html 嵌套列表解析为 perl 数组