How to Convert JSON to Array Using Laravel?

Hello friends, today I will tell you through experts tool tutorial How to Convert JSON to Array in Laravel? So let’s try to understand step to step with example.

json_decode() Function understands a JSON String. Suppose you have received some data in JSON form and you want to convert it into that PHP variable which can use it for the purpose of presenting that data to the user or for further programming.

app/Http/Controllers/MyDemoController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$jsonData = '[
{ "id": 1, "name": "gargi", "email": "gargi@gmail.com"},
{ "id": 2, "name": "jyoti", "email": "jyoti@gmail.com"},
{ "id": 3, "name": "sonam", "email": "sonam@gmail.com"}
]';
$data = json_decode($jsonData, true);
dd($data);
}
}

Output:

Array
(
[0] => Array
(
[id] => 1
[name] => gargi
[email] => gargi@gmail.com
)
[1] => Array
(
[id] => 2
[name] => jyoti
[email] => jyoti@gmail.com
)
[2] => Array
(
[id] => 3
[name] => sonam
[email] => sonam@gmail.com
)
)