基于根对象创建嵌套代码的问题

时间:2019-05-19 09:07:43

标签: php html

我正在创建一个基于oop类的代码,其中有一个包含多个数组的根变量(包括嵌套数组-下面的代码)。我想创建一个脚本,将根数据转换为页面上可用的命令。构造函数初始化第一个div容器,并且showchildren函数进行递归以覆盖无限数量的嵌套。这是我到目前为止所做的:

class Page
        {
            public $root = array(
                array(
                    "tag" => "div",
                    "id" =>  "ContainerId",
                    "class" =>  "ContainerClass",
                    "close" => ">",
                    "children" => array(
                        array(
                            "tag" => "div",
                            "id" =>  "inner",
                            "class" =>  "inner",
                            "close" => ">",
                            "children" => array(
                                array(
                                    "tag" => "div",
                                    "id" =>  "innerinner2",
                                    "class" =>  "innerinner2",
              ``                      "close" => ">",
                                ),
                                array(
                                    "tag" => "div",
                                    "id" =>  "innerinner3",
                                    "class" =>  "innerinner3",
                                    "close" => ">",
                                )
                            )
                        )
                    )
                )
            );

            public function __construct()
            {
                foreach ($this->root as $command) {
                    if (is_array($command)) {
                        foreach ($command as $key => $value) {
                            switch ($key) {
                                case "tag":
                                    echo "<$value ";
                                    break;
                                case "close":
                                    echo ">";
                                    break;
                                case "children":
                                    $this->showChildren($value);
                                    break;
                                default:
                                    echo "$key='$value' ";
                                    break;
                            }
                        }
                    }
                }
            }

            public function showChildren($childrenArray)
            {
                echo "showchildren called from children switch <br/>";
                if (is_array($childrenArray)) {
                    foreach ($childrenArray as $nestedCommand) {
                        if (is_array($nestedCommand)) {
                            foreach ($nestedCommand as $k => $v) {
                                switch ($k) {
                                    case "tag":
                                        echo "<$v ";
                                        break;
                                    case "close":
                                        echo ">";
                                        break;
                                    case "children":
                                        $this->showChildren($v);
                                        break;
                                    default:
                                        echo "$k='$v' ";
                                        break;
                                }
                            }
                        }
                    }
                }
            }
        }
        $pg = new Page();

问题在于在输出innerinner3中嵌套在innerinner2中:

我得到的是

 <div id="ContainerId" ...>
    <div id="innerinner1" ...>
      <div id="innerinner2" ...>
        <div id="innerinner3" ...>
        </div>
      </div>
    </div>
</div>

我想要得到的东西:

<div id="ContainerId" ...>
    <div id="innerinner1" ...>
      <div id="innerinner2" ...>

      </div>
      <div id="innerinner3" ...>

      </div>
    </div>
</div>

关闭标签可能有问题,但我等待您的回答! 关于这个问题的任何帮助,我们将不胜感激! 预先感谢!

0 个答案:

没有答案