【问题标题】:Multidimensional Array - How to access it in volt多维数组 - 如何以伏特访问它
【发布时间】:2015-10-10 15:21:02
【问题描述】:

好的,我创建了一个多维数组并将其存储在文章中

如果我这样做了

{{ dump(articles) }}

返回

array(2) {
  ["Comedy"]=>
  array(3) {
    [0]=>
    string(18) "Comedy Title1"
    [1]=>
    string(57) "Comedy Title2"
    [2]=>
    string(41) "Comedy Title3"
  }
  ["Horror"]=>
  array(3) {
    [0]=>
    string(18) "Horror Title1"
    [1]=>
    string(57) "Horror Title2"
    [2]=>
    string(41) "Horror Title3"
  }
}

现在我想要实现的是循环,打印标题,然后是每个部分的标题:

**Comedy**
Comedy Title1
Comedy Title2
Comedy Title3

**Horror**
Horror Title1
Horror Title2
Horror Title3

但是我可以访问标题没有问题,但似乎无法访问标题。

这是我目前所拥有的

{% for heading in articles %}
    {{ heading[loop.index0] }}
{% endfor %}

这将返回第一个部分的第一个值和第二个部分的第二个值

 comedy Title1
 horror Title2

如果我愿意

{% for heading in articles %}
    {% for title in heading %}
        {{ title }}<br />
    {% endfor %}
{% endfor %}

这会以正确的顺序返回所有标题,但没有标题:

Comedy Title1
Comedy Title2
Comedy Title3
Horror Title1
Horror Title2
Horror Title3

这很完美,但我只需要在每个数组的开头打印出标题,这就是我无法弄清楚的

我原以为它存储在标题部分,但 {{ heading }} 返回一个数组,{{ heading[0] }} 返回第一个标题。 {{articles }} 返回一个数组,{{articles[0] }} 甚至 {{article[0][0] }} 什么都不返回

我知道如何在常规 php 中执行此操作,但是我无法弄清楚电压,毫无疑问是一些简单的事情

【问题讨论】:

    标签: multidimensional-array phalcon volt


    【解决方案1】:

    我不熟悉 Volt,但根据 doc 尝试使用类似的东西:

     {% for key, heading in articles %}
       ** {{ key }} **<br />
            {% for title in heading %}
                {{ title }}<br />
            {% endfor %}
        {% endfor %}
    

    docs

    【讨论】:

    • 哇!是的,忘记了它实际上是关键而不是我正在访问的值。很有魅力
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多