flattenでcollectionを平坦化する

Laravelで多次元配列を1次元化するflatten()が便利だった.
連想配列の値のみを収集して1次元配列にしてくれる。


$ ./vendor/bin/sail artisan tinker
Psy Shell v0.11.1 (PHP 8.1.2 — cli) by Justin Hileman
>>> $collection = collect([ 'hoge' => [1, 2, 3],
... 'fuga' => 'aiueo',
... 'foo' => 1,
... 'bar' => null
... ]);
=> Illuminate\Support\Collection {#3529
     all: [
       "hoge" => [
         1,
         2,
         3,
       ],
       "fuga" => "aiueo",
       "foo" => 1,
       "bar" => null,
     ],
   }
>>> $collection->flatten();
=> Illuminate\Support\Collection {#3525
     all: [
       1,
       2,
       3,
       "aiueo",
       1,
       null,
     ],
   }