英文:
count key in twig symfony
问题
我尝试简单地计算城市中的对象
在巴黎,我有5个对象,我不想显示11111,只想显示5,
这是我的代码
{% set newAnnounceCity = [] %}
{% for item in announceCity %}
{% if item.infoCity is not null %}
{% if item.infoCity.city not in newAnnounceCity %}
<span class="font-semibold mr-2 text-left flex-auto">
<a href="{{path('app_city_show',{slug: item.infoCity.slug})}}">
<button class="m-2 p-2 pl-5 pr-5 bg-transparent border-2 border-red-500 text-red-500 text-lg rounded-lg hover:bg-gradient-to-b hover:from-red-600 hover:to-pink-500 hover:text-gray-100 focus:border-4 focus:border-red-300">
{{ (item.infoCity.city) }}
{% for key in item.infoCity.announce|keys %}
{{key|length }}
{% endfor %}
{% set newAnnounceCity = newAnnounceCity|merge([item.infoCity.city]) %}
</button>
</a>
</span>
{% endif %}
{% else %}
{% endif %}
{% endfor %}
有人可以帮助我吗?
我尝试计算一个键的对象数
英文:
i try to simply count object into a city
in paris, i have 5 object, i don't want show 11111 but just 5,
that my code
{% set newAnnounceCity = [] %}
{% for item in announceCity %}
{% if item.infoCity is not null %}
{% if item.infoCity.city not in newAnnounceCity %}
<span class=" font-semibold mr-2 text-left flex-auto">
<a href="{{path('app_city_show',{slug: item.infoCity.slug})}}">
<button class=" m-2 p-2 pl-5 pr-5 bg-transparent border-2 border-red-500 text-red-500 text-lg rounded-lg hover:bg-gradient-to-b hover:from-red-600 hover:to-pink-500 hover:text-gray-100 focus:border-4 focus:border-red-300">
{{ (item.infoCity.city) }}
{% for key in item.infoCity.announce|keys %}
{{key|length }}
{% endfor %}
{% set newAnnounceCity = newAnnounceCity|merge([item.infoCity.city]) %}
</button>
</a>
</span>
{% endif %}
{% else %}
{% endif %}
{% endfor %}
someone can help me
i try to count an keys object
答案1
得分: 2
用简单的代码替换这部分可能会更容易,但要猜测没有提供输入数组的结构有点困难。您可以尝试将原代码:
{% for key in item.infoCity.announce|keys %}
{{key|length }}
{% endfor %}
替换为:
{{ item.infoCity.announce|length }}
英文:
It's a bit hard to guess without a sample structure of your input array, but maybe try replacing
{% for key in item.infoCity.announce|keys %}
{{key|length }}
{% endfor %}
with simply
{{ item.infoCity.announce|length }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论