从集合中回显Laravel上的递归类别

时间:2017-02-08 07:31:05

标签: php laravel

我想以递归方式打印类别。我不知道父类别有多少孩子等等。实际上它是一个树形结构。

enter image description here

1 个答案:

答案 0 :(得分:0)

此代码剪切帮助我打印动态数组试试这个:

function makeNestedList(array $Array){
    $Output = '<ul>';
    foreach($Array as $Key => $Value){
        $Output .= "<li><strong>{$Key}: </strong>";
        if(is_array($Value)){
            $Output .= makeNestedList($Value);
        }else{
            $Output .= $Value;
        }
        $Output .= '</li>';
    }
    $Output .= '</ul>';
    return $Output;
}
$Data = array("Some Info" => array("A" => "a", "B" => array("B1" => "b1", "B2" => "b2"), "C" => array("C1" => array("C11" => "c11", "C12" => "c12", "C13" => array("C131" => "c131", "C132" => "c132")), "C2" => "c2")));
echo makeNestedList($Data);