I have this 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
]
}
And I want combined in only string the values, I need this:
"2638,2100,5407,2970,4481,1611,5345,50"
use implode https://laravel.com/docs/5.3/collections#method-implode
if $collection is the value you have shown then
dd($collection->implode(','));
should give the expected result
And if it's a multi-dimensional array, implode
can also accept first arg as the key name:
$collection = collect([
[ 'title' => 'Hello world' ],
[ 'title' => 'Another Hello world' ]
]);
$collection->implode('title', ',')
You can use PHP implode()
or Laravel ->implode()
method on collection:
implode(',', $collection->toArray());
$collection->implode(',');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With