19

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"

2 Answers 2

29

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', ',')
Sign up to request clarification or add additional context in comments.

Comments

6

You can use PHP implode() or Laravel ->implode() method on collection:

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

$collection->implode(',');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.