ItSolutionStuff.com

How to Convert String to Array Conversion in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Hi Developer,

I am going to explain to you example of laravel string to array conversion. We will use how to convert string to array in laravel. This article goes in detailed on how to convert string to array in laravel blade. In this article, we will implement a how to convert string to array in laravel blade.

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. you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions. so, lt's see one by one example

1. How to Convert String to Array in Laravel Controller

Controller File:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$string = "One, Two, Three, Four, Five";

$stringArray = explode(",", $string);

dd($stringArray);

}

}

Output:

array:5 [

0 => "One"

1 => " Two"

2 => " Three"

3 => " Four"

4 => " Five"

]

1. How to Convert String to Array in Laravel Blade

Blade File:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

</head>

<body>

@php

$string = "One, Two, Three, Four, Five";

$stringArray = explode(",", $string);

@endphp

<ul>

@foreach ($stringArray as $value)

<li>Value: {{ $value }}</li>

@endforeach

</ul>

</body>

</html>

Output:

One

Two

Three

Four

Five

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Install and Use Laravel Debugbar?

Read Now →

Laravel PayPal Send Payment to Email Example

Read Now →

How to Delete All Records Older Than 30 Days in Laravel?

Read Now →

Laravel Delete All Records Older Than 7 Days Example

Read Now →

Laravel Artisan Command to Create Migration and Model Example

Read Now →

Laravel Eloquent Always Load Model Relation Example

Read Now →

How to Stop Running Queue Jobs in Laravel?

Read Now →

Laravel Queue Process Timeout Error Solved

Read Now →

How to Define Fallback Routes in Laravel?

Read Now →

How to Show Pagination with Serial Number in Laravel?

Read Now →

How to Generate a Dropdown List of Timezone in Laravel?

Read Now →