10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Twig] 動的なキーで配列要素を取得する

Last updated at Posted at 2016-03-10

やりたかったこと

こんなことがやりたかった。

$arr = array('key' => 'hoge');
$key = 'key';
print $arr[$key]; // hoge

結論

attribute を使う
http://twig.sensiolabs.org/doc/functions/attribute.html

ex
{% set arr = {'key': 'hoge'} %}
{% set key = 'key' %}
{{attribute(arr, key)}} // 'hoge'
10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?