How To convert string to Array in Twig

2019-07-06 05:21发布

问题:

what i Need:

string as follows:

  ["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" }
  • Im using php slice function in such manner so that i should validate string till 5 string.
  • ex: Top End Transport System, ur Mid Level Transport System,The Introductory Transport System,In-Line Amplification Systems,Intelligent Ethernet Access System.
  • i need till first five string.
  • i have refer article http://twig.sensiolabs.org/doc/filters/split.html

here is twig code:

            {% set foo = item.Product_Name|split(',') %}
            {{ dump (foo) }}
           {% for i in item.Product_Name|slice(0, 5) %}
            {{ dump(i) }}
            {% endfor %}
  • slice function is for array so , please tell how to convert product name to array in twig

回答1:

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 %}


标签: symfony twig