Array To String Conversion Laravel

Array To String Conversion Laravel

Array To String Conversion Laravel

Hello Everyone, in this tutorial we will learn how can we do “Array to String Conversion Laravel”. So let’s get started.

In various situation we may need to convert Array to String in Laravel. When you have an Array of values but you want to store that array in database as a string. There are several methods. But easiest one is using the implode function. The implode() function return a string from a given array. Too much talking, lets jump into the coding part ( show me the code 😉 ).

Implode Function:

implode(separator, array);
Code:
Route::get('/arr-to-str', function() {
    $myHeroesArray = array('Batman', 'Hulk', 'Thor');
    return $myHeroesString = implode(', ', $myHeroesArray);
});

If you run this code, you will see an output like this

 

Batman, Hulk, Thor

 

Let’s say you have a Laravel Collections. You can also use that to…

Route::get('/arr-to-str', function () {
    $collection = collect([
        ['name' => 'John'],
        ['name' => 'Jane'],
    ]);
    return $collection->implode('name', ', ');
});

Result:

John, Jane

 

These are some easiest or cheesiest way of Array To String Conversion Laravel. See you another day!

 

Check Out More Resources:

Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-

WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/

Socials:

Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com

#Laravel #sql #mysql

 

That’s All. Feel free to knock me. Thanks.

Editorial Staff

A Learner and trying to share what I learned!