I have a weird problem with twig in Symfony2. I am using the following array:
[days] => Array
(
[1] => Array
(
[money] => 9
)
[2] => Array
(
[money] => 21
)
[3] => Array
(
[money] => 38
)
[4] => Array
(
[money] => 6
)
[18] => Array
(
[money] => 6
)
[19] => Array
(
[money] => 3
)
[31] => Array
(
[money] => 11
)
)
to test this I used the following code
{% for key in days %}
{{ key }}<br>
{% endfor %}
but the output shows the following
0
1
2
3
4
5
6
but it should look like this
1
2
3
4
18
19
31
Looks like twig creates a new array with new indexes. Is there a way to get the right index from array?
With var_dump($days)
in php I can see the right index, so the "problem" is related to twig.
The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.
The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
also you can try this :
{% for key,value in users %}
{{ key }}
{% endfor %}
or maybe look into the "loop" object defined in the for loop
http://twig.sensiolabs.org/doc/tags/for.html
Maybe this
http://www.twig-project.org/doc/templates.html
By default, a loop iterates over the values of the sequence. You can iterate on keys by using the keys filter:
<h1>Members</h1>
<ul>
{% for key in users|keys %}
<li>{{ key }}</li>
{% endfor %}
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With