仅在字符串中合并 Laravel collections 键

Merge Laravel collections keys in only string

我有这个 Laravel 5.3 tinny collection:

Collection {#325 ▼
  #items: array:8 [▼
    0 => 2638
    1 => 2100
    2 => 5407
    3 => 2970
    4 => 4481
    5 => 1611
    6 => 5345
    7 => 50
  ]
}

我只想将值组合成字符串,我需要这个:

"2638,2100,5407,2970,4481,1611,5345,50"

使用内爆https://laravel.com/docs/5.3/collections#method-implode

如果 $collection 是您显示的值,那么

dd($collection->implode(',')); 应该给出预期的结果

而且如果是多维数组,implode也可以接受第一个arg作为键名:

$collection = collect([
  [ 'title' => 'Hello world' ],
  [ 'title' => 'Another Hello world' ]
]);

$collection->implode('title', ',')

您可以在集合上使用 PHP implode() or Laravel ->implode() 方法:

implode(',', $collection->toArray());

$collection->implode(',');