如何在 Twig 中将字符串转换为数组 [英] How To convert string to Array in Twig

查看:57
本文介绍了如何在 Twig 中将字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么:

字符串如下:

  ["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 slice 函数,所以我应该验证字符串直到 5 个字符串.
  • 例如:高端传输系统、您的中级传输系统、介绍性传输系统、在线放大系统、智能以太网接入系统.
  • 我需要前五个字符串.
  • 我有参考文章 http://twig.sensiolabs.org/doc/filters/split.html
  • 这里是树枝代码:

                {% 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)

      You have to loop over your foo variable in order to print your products slice foo in order to get first five names foo|slice(0, 5)

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

      这篇关于如何在 Twig 中将字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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