Hi friends, in this tutorial you will learn JSON encode and decode in PHP. Before getting started with JSON with respect to PHP, you must know what JSON means exactly. JSON is nothing but a javascript object notation with a specific syntax or format as shown below.
{'key':value,'key1':value1,'key2':value2,'key3':value3}
Why we use JSON:- However, if we receive data from any kind of third-party application or via URL with the help of API then we can not use those data in PHP or we can not insert those data in our MySQL database. So, in order to overcome this problem, JSON helps us to convert those data into an associative array or indexed array by encode and decode functions so that we can access those data in the form of array values one by one and store them in php variables.
Also read, PHP JSON to Array with Example
Also, you can use foreach loop or for-loop to access the array values one by one. Finally, you can use those data in PHP and insert them in your MySQL database if you want.
PermalinkExamples of JSON encode and decode in PHP
json_encode():- This function converts a PHP array whether it may be an associative array or an indexed array into a JSON object.
Below is an example to convert an associative array to a JSON object
<?php
$employee_encode = array('Ram'=>'IT','Shyam'=>'Sales and Marketing','Jodhu'=>'HR Operations','Modhu'=>'Accounting');
echo "Output:";
echo '<br>';
echo '<br>';
echo json_encode($employee_encode);
?>
Output:-
{"Ram":"IT","Shyam":"Sales and Marketing","Jodhu":"HR Operations","Modhu":"Accounting"}
Below is an example to convert a PHP-indexed array to a JSON object
<?php
$employee1_encode = array('Ram','Shyam','Jodhu','Modhu');
echo '<br>';
echo '<br>';
echo "Output1:";
echo '<br>';
echo '<br>';
echo json_encode($employee1_encode);
echo '<br>';
echo '<br>';
?>
Output:-
["Ram","Shyam","Jodhu","Modhu"]
json_decode():- This function converts a JSON object to a PHP associative array.
Below is an example to convert a JSON object to a PHP array
<?php
//JSON decode
$employee_decode = '{"Ram":"IT","Shyam":"Sales and Marketing","Jodhu":"HR Operations","Modhu":"Accounting"}';
echo "Output:";
echo '<br>';
echo '<br>';
$result = json_decode($employee_decode);
print_r($result);
echo '<br>';
echo '<br>';
echo 'Now we will get the department of each employee from the above array values one by one';
echo '<br>';
echo '<br>';
echo $result->Ram;
echo '<br>';
echo $result->Shyam;
echo '<br>';
echo $result->Jodhu;
echo '<br>';
echo $result->Modhu;
?>
Output:-
stdClass Object ( [Ram] => IT [Shyam] => Sales and Marketing [Jodhu] => HR Operations [Modhu] => Accounting )
Now we will get the department of each employee from the above array values one by one
IT Sales and Marketing HR Operations Accounting
Also, we will get the department of each employee from the above array values one by one using foreach loop.
<?php
$employee_decode = '{"Ram":"IT","Shyam":"Sales and Marketing","Jodhu":"HR Operations","Modhu":"Accounting"}';
echo "Output:";
echo '<br>';
echo '<br>';
$result = json_decode($employee_decode);
print_r($result);
foreach($result as $key=>$value){
echo $value.'<br>';
}
?>
Output:-
IT Sales and Marketing HR Operations Accounting
Also read, Object in Javascript with example
Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.