string(362) "Top End Transport System,Band Combiner Devices,Our M-6ren">
gpt4 book ai didi

symfony - 如何在 Twig 中将字符串转换为数组

转载 作者:行者123 更新时间:2023-12-02 17:17:32 25 4
gpt4 key购买 nike

我需要什么:

字符串如下:

  ["Product_Name"]=> string(362) "Top End Transport System,Band Combiner Devices,Our Mid Level Transport System,The Introductory Transport System,In-Line Amplification Systems,Intelligent Ethernet Access System,Ethernet Access System,Intelligent Ethernet Access System (Ieas 05),Intelligent Ethernet Access System (Ieas 06),Intelligent Ethernet Access System (Ieas 03),Ethernet Aggregation Device" }
  • 我以这种方式使用 php 切片函数,以便我应该验证字符串直到 5 个字符串。
  • 例如:高端传输系统、中层传输系统、入门传输系统、串联放大系统、智能以太网接入系统。
  • 我需要直到前五个字符串。
  • 我有引用文章http://twig.sensiolabs.org/doc/filters/split.html

这是 Twig 代码:

            {% set foo = item.Product_Name|split(',') %}
{{ dump (foo) }}
{% for i in item.Product_Name|slice(0, 5) %}
{{ dump(i) }}
{% endfor %}
  • slice 函数是针对数组的,所以请告知如何将产品名称转换为 twig 中的数组

最佳答案

您必须循环 foo 变量才能打印产品切片 foo 以获得前五个名称 foo|slice(0, 5 )

{% set foo = item.Product_Name|split(',') %}
{% for i in foo|slice(0, 5) %}
{{ dump(i) }}
{% endfor %}

关于symfony - 如何在 Twig 中将字符串转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25262116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com