繁体   English   中英

获取twig中的数组索引#

[英]Get index # of array in twig

试图在twig中输出数组的索引#,在文档中找不到它。 有谁知道如何得到它?

array(2) {
  [0]=>
  array(2) {
    ["testimonial"]=>
    string(18) "Derby Heist Test 1"
    ["author"]=>
    string(6) "test 1"
  }
  [1]=>
  array(2) {
    ["testimonial"]=>
    string(18) "Derby Heist Test 2"
    ["author"]=>
    string(6) "test 2"
  }
}

所以我想在for循环中输出索引号0和1。 请帮忙。

您可以使用循环变量作为示例:

{% for user in users %}
    {{ loop.index }} - {{ user.username }}
{% endfor %}

loop.index循环的当前迭代。 (1索引)

loop.index0循环的当前迭代。 (0索引)

希望这有帮助

只需通过主数组,并指定您想要索引:

foreach($array as $index=>$arr) { ...

$ index现在可以满足您的需求。

或者通过TWIG:

{% for key,value in array_path %}
    Key : {{ key }}
    Value : {{ value }}
{% endfor %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM