4

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
3
  • i have t convert string in to array Commented Aug 12, 2014 at 10:55
  • 2
    Did u know a string is an array of chars? Commented Aug 12, 2014 at 11:00
  • 1
    Anyway, slice in twig also works with strings : The slice filter works as the array_slice PHP function for arrays and mb_substr for strings with a fallback to substr. Commented Aug 12, 2014 at 11:04

1 Answer 1

4

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 %}
Sign up to request clarification or add additional context in comments.

2 Comments

can help in simple issue - i have to remove (,) from last array a,b,c,d, so i have remove , after d .in my code - {{ dump(i) }}, so i have to remove last commas from there
@user3785613 try this {{ dump(i[:-1]) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.