PHP JSON Example - phptpoint

PHP JSON Example

/ins>

JSON Functions

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.

PHP json_encode( )

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.

Syntax

 
 

json_encode( ) Example 1

Output
{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}

 

json_encode( ) Example 2

 

Output
{“Name”:”Vineet”,”Profile”:”Developer”,”Mobile”:”9015501897″}

PHP json_decode( )

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.

Syntax

 

json_decode( ) Example 1

Output
object(stdClass)#1 (5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}array(5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}

json_decode( ) Example 2

 

 

Output
array(3)
{
[“Name”]=> string(6) “Vineet”
[“Profile”]=> string(9) “Developer”
[“Mobile”]=> string(10) “9015501897”
}