1
1

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で配列から値を検索する

Posted at

忘れてたのでメモとして

twig内でphpでいうところのin_arrayやarray_searchな判定をしたい!という場合はin を使うとよろしい。
https://twig.symfony.com/doc/2.x/templates.html#containment-operator

やりたいこと

配列の中に値があるか判定したいみたい。

phpの場合

$item = 'b';
$array = ['a','b','c'];

foreach($array as $a ){
  if($a === $item){
     echo 'あるっす';
  }
}
echo '無いっす';

これをTwigで書く時は以下のようにすると実現できる。

twigの場合

{{ item in array ? 'あったわ':'無いわ' }}

正直あまり使わないからすぐ忘れるのよね。。。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?