Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Symfony的4层嵌套数组_Php_Arrays_Symfony_Twig - Fatal编程技术网

Php 使用Symfony的4层嵌套数组

Php 使用Symfony的4层嵌套数组,php,arrays,symfony,twig,Php,Arrays,Symfony,Twig,这个数组是通过发送到Symfony从我的php输出的 Array ( [categories] => Array ( [0] => Array ( [name] => Localizar Peças [children] => Array (

这个数组是通过发送到Symfony从我的php输出的

    Array
(
    [categories] => Array
        (
            [0] => Array
                (
                    [name] => Localizar Peças
                    [children] => Array
                        (

                            [0] => Array
                                (
                                    [name] => GM (Chevrolet) 
                                    [href] => /index.php?route=product/category&path=130_64
                                    [children2] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Agile
                                                    [href] => /index.php?route=product/category&path=130_64_68
                                                    [children3] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [name] => Capô
                                                                    [href] => /index.php?route=product/category&path=130_68_76
                                                                )

                                                            [1] => Array
                                                                (
                                                                    [name] => Condensador
                                                                    [href] => /index.php?route=product/category&path=130_68_82
                                                                )

                                                            [2] => Array
                                                                (
                                                                    [name] => Farol
                                                                    [href] => /index.php?route=product/category&path=130_68_79
                                                                )

                                                            [3] => Array
                                                                (
                                                                    [name] => Lanterna
                                                                    [href] => /index.php?route=product/category&path=130_68_80
                                                                )

                                                            [4] => Array
                                                                (
                                                                    [name] => Painel Frontal
                                                                    [href] => /index.php?route=product/category&path=130_68_78
                                                                )

                                                            [5] => Array
                                                                (
                                                                    [name] => Para-choques
                                                                    [href] => /index.php?route=product/category&path=130_68_84
                                                                )

                                                            [6] => Array
                                                                (
                                                                    [name] => Para-lama
                                                                    [href] => /index.php?route=product/category&path=130_68_77
                                                                )

                                                            [7] => Array
                                                                (
                                                                    [name] => Radiador
                                                                    [href] => /index.php?route=product/category&path=130_68_81
                                                                )

                                                            [8] => Array
                                                                (
                                                                    [name] => Ventoinha
                                                                    [href] => /index.php?route=product/category&path=130_68_83
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [name] => Astra
                                                    [href] => /index.php?route=product/category&path=130_64_69
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [name] => Celta
                                                    [href] => /index.php?route=product/category&path=130_64_72
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [name] => Classic
                                                    [href] => /index.php?route=product/category&path=130_64_70
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [4] => Array
                                                (
                                                    [name] => Corsa
                                                    [href] => /index.php?route=product/category&path=130_64_71
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [5] => Array
                                                (
                                                    [name] => Cruze
                                                    [href] => /index.php?route=product/category&path=130_64_73
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [6] => Array
                                                (
                                                    [name] => Montana
                                                    [href] => /index.php?route=product/category&path=130_64_74
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                            [7] => Array
                                                (
                                                    [name] => Prisma
                                                    [href] => /index.php?route=product/category&path=130_64_75
                                                    [children3] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )


                        )

                    [column] => 1
                    [href] => /index.php?route=product/category&path=130
                )

        )

)
在.twig文件中,我无法汇编正确显示的代码,它只显示数组的第一级和第二级,如何显示第三级和第四级?我的.twig看起来像这样:

{% if categories %}
<div class="container">
  <nav id="menu" class="navbar">
    <div class="navbar-header"><span id="category" class="visible-xs">{{ text_category }}</span>
      <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
    </div>
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav navbar-nav">
        {% for category in categories %}
        {% if category.children %}
        <li class="dropdown"><a href="{{ category.href }}" class="dropdown-toggle" data-toggle="dropdown"><b>
          <div style="font-size: 15px;">{{ category.name }}</div>
          </b></a>
          <div class="dropdown-menu">
            <div class="dropdown-inner"> {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
              <ul class="list-unstyled">
                {% for child in children %}
                <li><a href="{{ child.href }}">{{ child.name }}</a>
                  <ul class="dropdown-menu sub-menu dropdown-inner">
                    {% for child2 in children2 %}
                    <li> <a href="{{ child2.href }}" >{{ child2.name }}</a> // level 3 here -> no works </li>
                    {% endfor %}
                  </ul>
                </li>
                {% endfor %}
              </ul>
              {% endfor %} </div>
            <a href="{{ category.href }}" class="see-all">{{ text_all }} {{ category.name }}</a> </div>
        </li>
        ....
        {% else %}
        <li><a href="{{ category.href }}">{{ category.name }}</a></li>
        {% endif %}
        {% endfor %}
      </ul>
    </div>
  </nav>
</div>
{% endif %} 
{%if类别%}
{{text_category}}
    {categories%%中的类别为%s} {%if category.children%}
  • {category.children | batch(category.children | length/category.column | round(1,'ceil'))%}
      {children%%中的children的百分比}
      • {children2%中child2的%
      • //此处为3级->无工作
      • {%endfor%}
    • {%endfor%}
    {%endfor%}
  • .... {%else%}
  • {%endif%} {%endfor%}
{%endif%}
输出应该是怎样的示例如下

Localizar Peças->GM(雪佛兰)->Agile->Cap

但按现在的方式,我只能在这里用.twig/html显示它

Localizar Peças->GM(雪佛兰)


我需要有人帮助我理解我做错了什么,因为在上面的示例中,它仍然没有显示数组的第三级。我认为我循环错误。

您没有循环正确的变量。在您的示例中,
children
是未知的,因为您尚未设置它

您可以执行以下操作:

{% for category in categories%}
 {% for child in category.children %}
  {{ child.name }}
 {% endfor %}
{% endfor %}

第二种方法是使用
set
将children变量指定给
category.children
。只有现在,您才能迭代这些

要在模板上进行4次或无限次循环,直到不再有子项,例如,您必须从模板中调用相同的细枝模板,如下所示:Category.html.twig

{% for child in category.children %}
  {% include "Category.html.twig" with { "category": child } %}
{% endfor %}

这将继续循环,直到所有类别及其子类别都被循环,这是有效的,因为我将变量category作为子类别传递,然后将在twig文件的下一次调用中使用。因此,子变量成为新的类别变量。

您没有循环正确的变量。在您的示例中,
children
是未知的,因为您尚未设置它

您可以执行以下操作:

{% for category in categories%}
 {% for child in category.children %}
  {{ child.name }}
 {% endfor %}
{% endfor %}

第二种方法是使用
set
将children变量指定给
category.children
。只有现在,您才能迭代这些

要在模板上进行4次或无限次循环,直到不再有子项,例如,您必须从模板中调用相同的细枝模板,如下所示:Category.html.twig

{% for child in category.children %}
  {% include "Category.html.twig" with { "category": child } %}
{% endfor %}

这将继续循环,直到所有类别及其子类别都被循环,这是有效的,因为我将变量category作为子类别传递,然后将在twig文件的下一次调用中使用。因此,子项成为新的类别变量。

好的,我删除了重复项,只留下这个,这更容易理解。谢谢。谢谢。好的,我删除了副本,只留下了这个,这样更容易理解。谢谢你。谢谢你。我一定是做错什么了。我不能显示第三级。我一定是做错了什么。我不能显示第三级。