从多维数组创建Html元素



我不知道如何解释这个,但是我们开始了

我有一个数组如下:

$arr = array(
    array('id' => 1,
        'content' => '<h1>This is a simple Content</h1>',
        'vars' => null,
        'path' => 'holders/row.php',
        'sub-page' => array(
            'id' => 2,
            'content' => '<h1>Sample Body Here</h1>',
            'vars' => '{"title":"Sample Title Here"}',
            'path' => 'holders/box.php',
            'sub-page' => array(
                'id' => 3,
                'vars' => '{"title":"Sample Title NUmber 3 Here"}',
                'content' => '<h1>Again Sample Body Here</h1>',
                'path' => 'holders/box.php'
            )
        )
    )
);

我要找的是数组应该去最深的sub-pagejson_decodevars键提取作为变量并包含文件。

一旦完成,输出将传递给父元素的内容键,并再次解析它

这主要用于创建动态内容

holders/row.php包含:

<div class="row">
    <?= $content; ?>
</div>

holders/box.php包含:

<div class="box lite">
    <div class="box-title">
        <h4><?= $title; ?></h4>
        <div class="tools">
            <a href="javascript:;" class="collapse">
                <i class="fa fa-chevron-up"></i>
            </a>
        </div>
    </div>
    <div class="box-body">
        <?= $content; ?>
    </div>
</div>

,期望的输出是:

<div class="row">
    <div class="box lite">
        <div class="box-title">
            <h4>Sample Title Here</h4>
            <div class="tools">
                <a href="javascript:;" class="collapse">
                    <i class="fa fa-chevron-up"></i>
                </a>
            </div>
        </div>
        <div class="box-body">
            <h1>Again Sample Body Here</h1>
            <div class="box lite">
                <div class="box-title">
                    <h4>Sample Title NUmber 3 Here</h4>
                    <div class="tools">
                        <a href="javascript:;" class="collapse">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>
                </div>
                <div class="box-body">
                    <h1>Again Sample Body Here</h1>
                </div>
            </div>
        </div>
    </div>
</div>

到目前为止我尝试了什么

我正在尝试循环数组,如下所示:

foreach ($arr as &$page) {
    if (isset($page[$keyToParse])) {
        $subMenu = $page[$keyToParse];
        unset($page[$keyToParse]);
        $page['content'] = $page['content'] . $this->parsePage($keyToParse, $subMenu);
        return $page['content'];
    } else {
        $params = strlen($page['vars']) > 0 ? json_decode($page['vars'], true) : [];
        $elementPath = $page['path'];
        $params = array_merge($params, array('content' => $page['content']));
        $page['content'] = callback(function () use ($params, $elementPath) {
            extract($params);
            include($elementPath);
        });
        return $page['content'];
    }

和函数回调是:

function callback($userfunc)
{
    ob_start();
    $userfunc();
    $return = ob_get_contents();
    ob_end_clean();
    return $return;
}

请帮助我,因为我现在被困了很长时间

你所要做的就是这样。

function renderNode($node){
        ob_start();
        if (isset($node["sub-page"]) && $node["sub-page"]){
                $node["content"] = renderNode($node["sub-page"]);
        }
        extract($node);
        include ($node["path"]);
        return ob_get_clean();
}

,然后调用它:

echo renderNode($arr[0]);

相关内容

  • 没有找到相关文章

最新更新