Home >>Json Tutorial >PHP JSON Example
Function | Description |
json_encode( ) | Returns the JSON representation of a value |
json_decode( ) | Decodes a JSON string. |
json_last_error( ) | Returns the last error occurred. |
In PHP, json_encode( ) function is used to encode JSON into PHP. In other words we can say that json_encode( ) function convert PHP variables into JSON String.
string json_encode(mixed value,[int options=0]
Ex 1
<?php $array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($array); ?>
Ex 2
<?php $array = array("Name"=>"Vineet","Profile"=>"Developer","Mobile"=>"9015501897"); echo json_encode($array); ?>
In PHP json_decode( ) function is used to decode JSON into PHP. In other words we can say that json_decode( ) function convert JSON string into PHP variables.
mixed json_decode(string json,[bool assoc = false,[int depth=512]]
Ex 1
<?php $json1 = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json1)); var_dump(json_decode($json1, true)); ?>
Ex 2
<?php $json2 = '{"Name":"Vineet","Profile":"Developer","Mobile":"9015501897"}'; var_dump(json_decode($json2, true)); ?>