PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] Getting array keys in Twig? (Symfony)

 January 10, 2022     arrays, php, symfony, twig     No comments   

Issue

Is it possible to get the key of an array in Twig (in Symfony)?

For example if I have an array of:

array(
'key1' => 'value1',
'key2' => 'value2',
);

Is it possible in Twig to print:

key1: value1

key2: value2

Thanks


Solution

Try following format:

{% for key, value in array %}
    {{ key }} - {{ value }}
{% endfor %}

More Information on Offical Twig about Iterating over Keys and Values

https://twig.symfony.com/doc/3.x/tags/for.html#iterating-over-keys-and-values



Answered By - Bora
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing