2

I have an array in my controller. I pass this to my twig view along with another array one

 user_id = Array ( [0] => abc [1] => Def [2] => Hij )

 data =  [0] => HelpCenterBundle\Entity\New Object
    (
        [id:HelpCenterBundle\Entity\New:private] => 5
        [userId:HelpCenterBundle\Entity\New:private] => 314
        [comment:HelpCenterBundle\Entity\New:private] => 1
    )

I want to print it in a table.

    {% for countlist in data %}
        <tr>
          <td>{{ countlist.id }}</td>
       </tr>
       <tr>
         <td> here i want to print first element of user_count </td>
      </tr>
    {% endfor %}

I tried with a for loop like

  {% for first in user_id %}
   <td>{{ first }}</td>

But it results in all the contents in a same line. Please help

1
  • Is not so clear what you want to achieve: only the first element of the user_id array? try with {{ user_id[0] }} otherwise try to made an example of what is your goal Commented Jun 12, 2018 at 10:40

1 Answer 1

3

seems you're trying to pass an array of objects to twig, which can still be done like so:

{% for key,value in array %}
    {% value.objectProperty %}
{% endfor %}

if you want the count of something you can do this:

{% value|length %}

it might also help for building html using arrays in twig to dump your values to see what you work with:

{{ dump(array) }}

this shows a nice pretty format of data that's passed to your template.

Sign up to request clarification or add additional context in comments.

Comments

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.