将多维数组提取到嵌套列表中

时间:2013-10-15 09:18:20

标签: php arrays recursion multidimensional-array nested-lists

我正在尝试通过插件的Ordered List结构将Nestable Plugin中的多维数组提取到HTML

我建立的递归并不适用,因为我创建的OL标签位于有孩子的LI之外。乍一看,这看起来很简单,但我没能完成它。

这是我到目前为止所写的,我无法理解如何检查递归中的下一个循环是否是有子代的LI,所以{{1将在里面,使用按钮和句柄OL
(插件的HTML结构示例见下文)

div

这是我的功能输出:

function buildNestedList($a) {

    if (!is_array($a)) {
        echo ('<li class="dd-item" data-name="'.$a.'"><div class="dd-handle">'.$a.'</div></li>');
        return;
    }

    foreach($a as $k => $v) {
        if(($k=="children")&&(is_string($k))) {
            echo ('<ol class="dd-list">');
        buildNestedList($v);
            echo ("</ol></li>");
        }
        else {
            printAll($v);
        }
    }
}

这是我从DB获得的<div class="dd" id="nestable"> <ol class="dd-list"> <li class="dd-item" data-name="item 1"> <div class="dd-handle">item 1</div> </li> <li class="dd-item" data-name="item 2"> <div class="dd-handle">item 2</div> </li> <li class="dd-item" data-name="item 3"> <div class="dd-handle">item 3</div> </li> <ol class="dd-list"> <li class="dd-item" data-name="item 4"> <div class="dd-handle">item 4</div> </li> <li class="dd-item" data-name="item 5"> <div class="dd-handle">item 5</div> </li> <li class="dd-item" data-name="item 6"> <div class="dd-handle">item 6</div> </li> <ol class="dd-list"> <li class="dd-item" data-name="item 7"> <div class="dd-handle">item 7</div> </li> <li class="dd-item" data-name="item 8"> <div class="dd-handle">item 8</div> </li> </ol> </ol> <li class="dd-item" data-name="item 9"> <div class="dd-handle">item 9</div> </li> <li class="dd-item" data-name="item 10"> <div class="dd-handle">item 10</div> </li> <li class="dd-item" data-name="item 11"> <div class="dd-handle">item 11</div> </li> </ol> </div> 示例:

Array

这是正确的插件的Array ( [0] => Array ( [name] => item 1 ) [1] => Array ( [name] => item 2 ) [2] => Array ( [name] => item 3 [children] => Array ( [0] => Array ( [name] => item 4 ) [1] => Array ( [name] => item 5 ) [2] => Array ( [name] => item 6 [children] => Array ( [0] => Array ( [name] => item 7 ) [1] => Array ( [name] => item 8 ) ) ) ) ) [3] => Array ( [name] => item 9 ) [4] => Array ( [name] => item 10 ) [5] => Array ( [name] => item 11 ) ) 结构示例:

HTML

1 个答案:

答案 0 :(得分:1)

您必须正确打开和关闭标签。

使用此:

$source=json_decode('[{"name":"item 1"},{"name":"item 2"},{"name":"item 3","children":[{"name":"item 4"},{"name":"item 5"},{"name":"item 6","children":[{"name":"item 7"},{"name":"item 8"}]}]},{"name":"item 9"},{"name":"item 10"},{"name":"item 11"}]',true);

function buildNestedList(array $a)
{
    if(empty($a["name"]))
    {
        echo "<ol>";
        foreach($a as $item)
        {
            buildNestedList($item);
        }
        echo "</ol>";
    }
    else
    {
        echo "<li><div>".$a["name"]."</div>";
        if(!empty($a["children"]))
        {
            buildNestedList($a["children"]);
        }
        echo "</li>";
    }
}
buildNestedList($source);

PHP Live demo

请注意,我在上面的现场演示中打印了一下。

以下是生成的嵌套列表的简单示例:http://jsfiddle.net/YDnd8/