LoginSignup
2
1

添字の無いCollectionに値の存在判定を行う

Posted at

概要

Collectionで特定の値の存在判定をする際こんな感じにcontainsを使うと思います

$result = collect([
    [
        'hoge' => 3,
    ],
    [
        'hoge' => 2,
    ],
])->contains('hoge', 3);
//true

以下のようなCollectionの場合、困りますよね

collect([3,2]);

こういう場合はクロージャを用いてこう書きます

$result = collect([3,2])->contains(fn(int $value) => $value === 3);
//true
2
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
2
1