Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index # of array in twig

Tags:

php

twig

Trying to output the index # of an array in twig, having trouble finding it in the docs. Anyone know how to get it?

array(2) {
  [0]=>
  array(2) {
    ["testimonial"]=>
    string(18) "Derby Heist Test 1"
    ["author"]=>
    string(6) "test 1"
  }
  [1]=>
  array(2) {
    ["testimonial"]=>
    string(18) "Derby Heist Test 2"
    ["author"]=>
    string(6) "test 2"
  }
}

so I'd like to output the index numbers 0 and 1 in a for loop. Please help.

like image 268
Brendan Jackson Avatar asked Jan 24 '17 16:01

Brendan Jackson


1 Answers

You can use The loop variable as example:

{% for user in users %}
    {{ loop.index }} - {{ user.username }}
{% endfor %}

loop.index The current iteration of the loop. (1 indexed)

loop.index0 The current iteration of the loop. (0 indexed)

Hope this help

like image 92
Matteo Avatar answered Oct 21 '22 10:10

Matteo