LoginSignup
0
0

More than 3 years have passed since last update.

Laravel の Collection で多次元配列の最初or最後の値を得る

Posted at

Laravelで多次元配列の最初or最後の要素を取得する方法をシェアします。

次のようにpluckメソッドで指定したキーの全コレクション値を取得し、first()をチェーンでつなぎ、最初の値を取得できます。

$collection = collect([
    ['id' => '001', 'name' => '鈴木'],
    ['id' => '002', 'name' => '山田'],
    ['id' => '003', 'name' => '田中'],
    ['id' => '004', 'name' => '佐藤'],
]);

echo $collection->pluck('name')->first(); // 鈴木

echo $collection->pluck('name')->last(); // 佐藤

参考

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