How to Convert JSON String to Array in PHP?

03-Apr-2023

.

Admin

How to Convert JSON String to Array in PHP?

Hi Dev,

This post will give you example of how to convert json string to array in php. This post will give you simple example of php convert json string to array. you will learn convert json string to array in php. it's simple example of php string to json array code example. you will do the following things for how do i convert json to php array?.

There ara two example to Convert to json String to Array in php. in the this example, we will use json_decode()to Convert json string to Array.Let's see both examples with output.

Example 1:


index.php

<?php

// JSON string

$jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

// Convert JSON string to Array

$arrayData = json_decode($jsonData, TRUE);

// print Array

print_r($arrayData);

?>

Output:

Array

(

[a] => 1

[b] => 2

[c] => 3

[d] => 4

[e] => 5

)

Example 2:

index.php

<?php

// JSON string

$jsonData ='[

{"id":"1","name":"Hardik Savani"},

{"id":"2","name":"Jaydeep Pathar"},

{"id":"3","name":"Nikhil Thummer"}

]';

// Convert JSON string to Array

$arrayData = json_decode($jsonData, true);

// print Array

print_r($arrayData);

?>

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Hardik Savani

)

[1] => Array

(

[id] => 2

[name] => Jaydeep Pathar

)

[2] => Array

(

[id] => 3

[name] => Nikhil Thummer

)

)

I hope it could help you...

#PHP