filmov
tv
PHP and JSON Encode and decode examples

Показать описание
JSON stands for JavaScript Object Notation. It is a data interchange format.
PHP has built-in functions json_encode() and json_decode() to deal with JSON data.
We are discussing these two functions with examples.
json_encode() is used to convert PHP data into JSON format. In this example PHP associative array is converted into a JSON object. The output of JSON format of this array looks like this {"John":35,"Tom":28,"David":32}.
$person = [ "John" =gt; 35, "Tom" =gt; 28, "David" =gt; 32 ];
echo json_encode($person);
json_decode() is just the reverse of json_encode. json_decode() function converts JSON format into PHP data. We are converting the previous output back to PHP data. The output looks like an associative PHP array [ "John" =gt; 35, "Tom" =gt; 28, "David" =gt; 32 ].
$json = '{"John":35,"Tom":28,"David":32}';
$person = json_decode($json);
PHP has built-in functions json_encode() and json_decode() to deal with JSON data.
We are discussing these two functions with examples.
json_encode() is used to convert PHP data into JSON format. In this example PHP associative array is converted into a JSON object. The output of JSON format of this array looks like this {"John":35,"Tom":28,"David":32}.
$person = [ "John" =gt; 35, "Tom" =gt; 28, "David" =gt; 32 ];
echo json_encode($person);
json_decode() is just the reverse of json_encode. json_decode() function converts JSON format into PHP data. We are converting the previous output back to PHP data. The output looks like an associative PHP array [ "John" =gt; 35, "Tom" =gt; 28, "David" =gt; 32 ].
$json = '{"John":35,"Tom":28,"David":32}';
$person = json_decode($json);