Twig 忘记了数组键
我在 Symfony2 中遇到了一个关于 twig 的奇怪问题。我正在使用以下数组:
[days] => Array
(
[1] => Array
(
[money] => 9
)
[2] => Array
(
[money] => 21
)
[3] => Array
(
[money] => 38
)
[4] => Array
(
[money] => 6
)
[18] => Array
(
[money] => 6
)
[19] => Array
(
[money] => 3
)
[31] => Array
(
[money] => 11
)
)
为了测试这一点,我使用了以下代码
{% for key in days %}
{{ key }}<br>
{% endfor %}
,但输出显示以下内容
0
1
2
3
4
5
6
,但它应该看起来像这样
1
2
3
4
18
19
31
看起来 twig 创建了一个带有新索引的新数组。有没有办法从数组中获取正确的索引?
使用 php 中的 var_dump($days) ,我可以看到正确的索引,因此“问题”与树枝有关。
I have a weird problem with twig in Symfony2. I am using the following array:
[days] => Array
(
[1] => Array
(
[money] => 9
)
[2] => Array
(
[money] => 21
)
[3] => Array
(
[money] => 38
)
[4] => Array
(
[money] => 6
)
[18] => Array
(
[money] => 6
)
[19] => Array
(
[money] => 3
)
[31] => Array
(
[money] => 11
)
)
to test this I used the following code
{% for key in days %}
{{ key }}<br>
{% endfor %}
but the output shows the following
0
1
2
3
4
5
6
but it should look like this
1
2
3
4
18
19
31
Looks like twig creates a new array with new indexes. Is there a way to get the right index from array?
With var_dump($days)
in php I can see the right index, so the "problem" is related to twig.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你也可以尝试这个:
或者查看 for 循环中定义的“loop”对象
http ://twig.sensiolabs.org/doc/tags/for.html
also you can try this :
or maybe look into the "loop" object defined in the for loop
http://twig.sensiolabs.org/doc/tags/for.html
也许这个
http://www.twig-project.org/doc/templates.html
默认情况下,循环会迭代序列的值。您可以使用键过滤器迭代键:
Maybe this
http://www.twig-project.org/doc/templates.html
By default, a loop iterates over the values of the sequence. You can iterate on keys by using the keys filter:
它与twig无关,这是使用embeddedDocuments的学说-mongodb中的一个已知差距。 Doctrine 无法处理嵌入文档中的键值,它会从 0 开始重新排序键,从而忽略正确的键值。不管怎样,谢谢你的帮助:)。
It's not related on twig, it's a known gap in doctrine-mongodb using embeddedDocuments. Doctrine can't handle key-Values from embeddedDocuments, it will reorder the keys beginning from 0 and will so ignore the right key-values. Thank you anyway for your help :).