How To Convert JSON String To Array

I am going to show you how to json data in array. You can Use PHP default json_decode() funcation to objects are converted to standard objects. When use pass TRUE Parameters in json_decode(), returned objects will be converted into associative arrays. In below i gave you both example.

Example1

Standard Objects.


$json = '{"name":"sonu","class":"10th","rollnumber":1113,"address":"delhi","subject":"Hindi"}';

// get data in object
var_dump(json_decode($json)); 

//output
object(stdClass)#1 (5) { ["name"]=> string(4) "sonu" ["class"]=> string(4) "10th" ["rollnumber"]=> int(1113) ["address"]=> string(5) "delhi" ["subject"]=> string(5) "Hindi" }

Example2

Associative Arrays


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

// get data in array
var_dump(json_decode($json, true));

//output
array(5) { ["name"]=> string(4) "sonu" ["class"]=> string(4) "10th" ["rollnumber"]=> int(1113) ["address"]=> string(5) "delhi" ["subject"]=> string(5) "Hindi" }

Leave a Reply

Your email address will not be published. Required fields are marked *