PHP Tutorial PHP Advanced PHP References

PHP - JSON



JavaScript Object Notation (JSON) is a language-independent data format for storing and exchanging data. It is a text-based format and it can easily be sent to and from a server, and used as a data format by any programming language. In case of PHP language, JSON is a text which encodes PHP objects as JSON, sends JSON to the server and decodes back into PHP objects.

JSON in PHP

PHP has JSON extension which implements the JavaScript Object Notation (JSON) data-interchange format. This extension is bundled and compiled into PHP by default.

json_encode() function

The PHP json_encode() function returns a string containing the JSON representation of the given value.

Example: json_encode() example

The example below shows how to use the json_encode() function with a PHP array.

<?php
//declaring an array
$arr = array('a' => 10, 'b' => 20, 'c' => 30);

//using json_encode() function to 
//encode into JSON string
$jsonObj = json_encode($arr);

//displaying the result
echo $jsonObj;
?>

The output of the above code will be:

{"a":10,"b":20,"c":30}

Example: json_encode() example with PHP object

The example below shows how to use this function with a PHP object.

<?php
//declaring a class
class person {     
}
   
//declaring an object
$p1 = new person();
   
//setting the object elements
$p1->name = "John";
$p1->email = "john@example.com";

//using json_encode() function to 
//encode into JSON string
$jsonObj = json_encode($p1);

//displaying the result
echo $jsonObj;
?>

The output of the above code will be:

{"name":"John","email":"john@example.com"}

json_decode() function

The PHP json_decode() function decodes a JSON encoded string and converts it into a PHP variable. If the optional second parameter is set to true, JSON objects will be returned as associative arrays. If set to false, JSON objects will be returned as objects.

Example: json_decode() example

The example below shows the usage of json_decode() function.

<?php
//declaring a JSON string
$jsonObj = '{"a":10,"b":20,"c":30}';

//using json_decode() function to 
//decode the JSON string
var_dump(json_decode($jsonObj));
echo "\n";
var_dump(json_decode($jsonObj, true));
?>

The output of the above code will be:

object(stdClass)#1 (3) {
  ["a"]=>
  int(10)
  ["b"]=>
  int(20)
  ["c"]=>
  int(30)
}

array(3) {
  ["a"]=>
  int(10)
  ["b"]=>
  int(20)
  ["c"]=>
  int(30)
}

Example: accessing object properties

The example below shows how to access properties from the PHP object.

<?php
//declaring a JSON string
$jsonObj = '{"a":10,"b":20,"c":30}';

//using json_decode() function to 
//decode the JSON string
$obj = json_decode($jsonObj);

echo $obj->a;
echo "\n";
echo $obj->b;
echo "\n";
echo $obj->c;
?>

The output of the above code will be:

10
20
30

Example: accessing values from associative array

The example below shows how to access values from the PHP associative array.

<?php
//declaring a JSON string
$jsonObj = '{"a":10,"b":20,"c":30}';

//using json_decode() function to 
//decode the JSON string
$arr = json_decode($jsonObj, true);

echo $arr['a'];
echo "\n";
echo $arr['b'];
echo "\n";
echo $arr['c'];
?>

The output of the above code will be:

10
20
30

Example: looping through the values

The foreach() can be used to access all values.

<?php
//declaring a JSON string
$jsonObj = '{"a":10,"b":20,"c":30}';

//using json_decode() function to 
//decode the JSON string as object
$obj = json_decode($jsonObj);

//using foreach loop to access all values
foreach($obj as $key => $value) {
  echo $key ." => ". $value ."\n";
}

echo "\n";

//using json_decode() function to 
//decode the JSON string as array
$arr = json_decode($jsonObj, true);

//using foreach loop to access all values
foreach($arr as $key => $value) {
  echo $key ." => ". $value ."\n";
}
?>

The output of the above code will be:

a => 10
b => 20
c => 30

a => 10
b => 20
c => 30

Complete PHP JSON Reference

For a complete reference of all PHP JSON functions, see the complete PHP JSON Reference.


5