In this tutorial we’re going to learn how to convert string to Array Conversion in laravel. This tutorial will give you a simple example of string to array conversion in laravel.
I will give you the following two examples of laravel string to array conversion in controller file and blade file.
- 1. How to Convert String to Array in Laravel Controller
- 2. How to Convert String to Array in Laravel Blade
we will use explode() to convert string into array in laravel.
1st example go to your controller and use below function
public function index()
{
$string = "Amit, Sumit, Monu, Rahul, Raju";
$stringArray = explode(",", $string);
dd($stringArray);
}
2nd example go to your blade page and put below code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
@php
$string = "Amit, Sumit, Monu, Rahul, Raju";
$stringArray = explode(",", $string);
@endphp
<ul>
@foreach ($stringArray as $value)
<li>Value: {{ $value }}</li>
@endforeach
</ul>
</body>
</html>
Output:-
Now all data is coming successfully. 👍👍