计算数组中元素的数量 [英] Counting the number of elements in array

查看:37
本文介绍了计算数组中元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算 Twig 数组中的条目数.这是我试过的代码:

I am looking to count the number of entries I have in an array in Twig. This is the code I've tried:

{%for nc in notcount%}
{{ nc|length }}
{%endfor%}

然而,这只会产生数组中一个值的字符串长度.

This however only produces the length of the string of one of the values in the array.

{{nc}} 将生成数组所有值的输出(有 2 个),但我希望输出只是数字 2(计数)而不是所有的数组中的信息.

{{nc}} will produce an output of all the values of the array (there are 2) but I want the output to be just the number 2 (the count) and not all the information in the array.

推荐答案

只需使用长度过滤器在整个阵列上.它不仅适用于字符串:

Just use the length filter on the whole array. It works on more than just strings:

{{ notcount|length }}

这篇关于计算数组中元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆