How To Convert JSON To Array using PHP

By | January 4, 2017

Today we will see how to convert json to array using PHP inbuild function. Many times in our project we have a requirement to convert json string to array or we may need to convert array to json data. We will cover both using PHP. In the follow up tutorial we will also cover using Javascript.

convert json to array

What is JSON File Format?

JSON stands for JavaScript Object Notation and is a light weight data exchange format. It is more readable as compared to XML and is a well known data exchange format in the form of Key value pairs. JSON file contains information stored in JSON format and has the extension of *.json. Now we may need to parse our data if we want to transfer our data using a web service to android or iOS app we can send data using JSON because it will not understand PHP raw data. So for that reason we may need to convert our data from array to json.

Check Convert Data from MySQL to JSON

How to Convert JSON To Array using PHP


<?php

$json_string = '{"Name":"Techblog","Category":"Tech","Year":2016}';
$json_array = json_decode($json_string, TRUE);

//to print array
print_r($json_array);
?>

Convert Array To JSON using PHP


<?php

$json_array = array(

'Name' => 'TechPlugg',
'Category' => 'Tech',
'Year'=>'2016'
);

echo json_encode( $json_array);

?>

So this was simple tutorial to convert json to array and vice versa using PHP. If you like this tutorial do let us know in comments. We will add more such tutorials. Also check How to get JSON data in PHP.