PHP How can close the html tags from this array?

☆樱花仙子☆ 提交于 2019-12-08 15:22:40

Nesting arrays and using recursion may look like this:

<?php
$data = [
    [
        'tag' => 'form',
        'options' => ' class="form-horizontal"',
        'text' => false,
        'items' => [
            [
                'tag' => 'div',
                'options' => ' class="firstclass"',
                'text' => 'FIRSTCLASS',
            ],
            [
                'tag' => 'div',
                'options' => ' class="form-group"',
                'text' => 'FORMGROUP',
                'items' => [
                    [
                        'tag' => 'label',
                        'options' => ' class="col-sm-3 control-label"',
                        'text' => 'TEXT',
                        'items' => [
                            [
                                'tag' => 'div',
                                'options' => ' class="col-sm-9"',
                                'items' =>[
                                    [
                                        'tag' => 'span',
                                        'options' => ' class="myspan"',
                                        'text' => 'MYSPAN',
                                        'items' => [
                                            [
                                                'tag' => 'div',
                                                'options' => ' class="myclass"',
                                                'text' => 'MYCLASS',
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            [
                'tag' => 'div',
                'options' => ' class="firstclass"',
                'text' => 'FIRSTCLASS',
            ],
            [
                'tag' => 'div',
                'options' => ' class="form-group"',
                'text' => 'FORMGROUP',
                'items' => [
                    [
                        'tag' => 'label',
                        'options' => ' class="col-sm-3 control-label"',
                        'text' => 'TEXT',
                        'items' => [
                            [
                                'tag' => 'div',
                                'options' => ' class="col-sm-9"',
                                'items' => [
                                    [
                                        'tag' => 'span',
                                        'options' => ' class="myspan"',
                                        'text' => 'MYSPAN',
                                        'items' => [
                                            [
                                                'tag' => 'div',
                                                'options' => ' class="myclass"',
                                                'text' => 'MYCLASS',
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            [
                'tag' => 'div',
                'options' => ' class="firstclass"',
                'text' => 'FIRSTCLASS',
            ],
        ],
    ],
];

And code for generating html like this:

<?php
function get($data) {
    $result = '';
    foreach ($data as $key => $row) {
        $tag = array_key_exists('tag', $row) ? $row['tag'] : false;
        $text = array_key_exists('text', $row) ? $row['text'] : '';
        $options = array_key_exists('options', $row) ? $row['options'] : '';
        $nested = array_key_exists('items', $row) ? get($row['items'])) : '';

        if (false === $tag) {
            continue; // May throw exception or some error handling
        }

        $result .= "<{$tag} {$options}>{$text}{$nested}</{$tag}>";
    }

    return $result;
}

echo(get($data));

To nest your original data here is the nesting function:

function nest(array $data = array()) {
    $level = null;
    $last = null;
    foreach ($data as $index => $item) {
        $level = $level ?? $item['level'];
        if (null !== $last && $item['level'] > $level) {
            $data[$last]['items'][] = $item;
            unset($data[$index]);
        }
        if ($item['level'] == $level) {
            $last = $index;
        }
    }
    foreach ($data as $index => $item) {
        if (array_key_exists('items', $item)) {
            $data[$index]['items'] = nest($item['items']);
        }
    }

    return $data;
}

print_r(nest($data));die();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!