PHP JSON Example

This section covers how to encode and unravel JSON objects utilizing PHP programming dialect.

PHP permits us to encode and decipher JSON by the assistance of json_encode() and json_decode functions.

PHP json_encode

PHP grants us to encode and decode JSON by the help of json_encode() and json_decode functions.

string json_encode ( $value [, $options = 0 ] )

Parameters

Value- The worth being encoded. This capacity just works with UTF-8 encoded information.

Options- This discretionary quality is a bitmask comprising of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT.

PHP json_encode example

	
 	<?php  
 	$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);  
 	echo json_encode($arr);  
 	?> 
 	

PHP json_decode

The json_decode() capacity interprets the JSON string. At the end of the day, it changes over JSON string into a PHP variable.

mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

Paramaters

json_string − It is an encoded string which must be UTF-8 encoded information.

assoc − It is a boolean sort parameter, when set to TRUE, returned items will be changed over into acquainted arrays.

profundity − It is a whole number sort parameter which determines recursion profundity

alternatives − It is a whole number sort bitmask of JSON decipher, JSON_BIGINT_AS_STRING is supported

PHP json_decode example

	
 	<?php  
 	$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';  
 	var_dump(json_decode($json, true 
 	?>