json_encode() & json_decode() in PHP

 






json_encode():

json_encode() is one of the powerful in built function in PHP. You can encode an array into JSON object. JSON stands for JavaScript Object Notation & it is very lightweight to store and transport data.And more importantly building API is more easy and self describing because of JSON object. Now, we will see a example how can we convert an associative array into a JSON object:

<?php
$person = array("Name"=>'Konok'"Age"=>25"Country"=>'Bangladesh');

echo json_encode($person);
?>

The result will be : {"Name":"Konok","Age":25,"Country":"Bangladesh"}

json_decode():

json_decode() function is used to decode or convert a JSON object to a PHP object. Now, we will see a example below:

<?php
$person = '{"Name":"Konok","Age":25,"Country":"Bangladesh"}';

var_dump(json_decode($person));
?>

The result will be : object(stdClass)#1 (3) { ["Name"]=> string(5) "Konok" ["Age"]=> int(25) ["Country"]=> string(10) "Bangladesh" }


Post a Comment (0)
Previous Post Next Post