Studyon Minte9.com
ZCE 5.3




JSON



	// Javascript Object Notation / Data interchange format
	// Extension loaded by default in PHP
	// json_decode ($json, $assoc = false, $depth)
	// json_encode ($json, $options)
	// json_last_error()


JSON (JavaScript Object Notation) it is the actual form that a JavaScript engine will map the object to in 
memory. PHP have native support for JSON.

* json_decode

	<?php
		/**
		* json_decode ($json, $assoc = false, $depth)
		* 
		* Return the json representation of a value
		* 
		* This function only works with UTF-8 encoded data.
		* 
		* Retursn NULL - if the json cannot be decoded
		* Retursn NULL - if the encoded data is deeper than the recursion limit.
		*
		*/
		
		$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
		var_dump(json_decode($json));
			// object(stdClass) { "a"=> 1, "b"=>2, ... }
		var_dump(json_decode($json, true));
			// array(5) { "a"=> 1, "b"=>2, ... }

		$json = '';
		var_dump(json_decode($json));
			// NULL


* json_encode
	
	<?php
		/**
		* string json_encode ( mixed $value )
		*
		* Return the json representation of a value
		* 
		* This function only works with UTF-8 encoded data
		* 
		* Returns a JSON encoded string on success or FALSE on failure
		*
		*/
		
		$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
		echo json_encode($arr);
			// Outputs: {"a":1,"b":2,"c":3,"d":4,"e":5}


* json_last_error - Return the last error occured

	<?php
		/**
		* int json_last_error ( void )
		* 
		* Returns an integer, the value can be one of the JSON error codes constants
		*
		*/
		
		// A valid json string
		$json = '{"a": "b"}';
		json_decode($json);
		echo json_last_error();
			// Outputs: 0 (no error)

		// An invalid json string
		$json = "{'a': 'b'}";
		json_decode($json);
		echo json_last_error();
			// Outputs: 4 (JSON_ERROR_SYNTAX)


http://php.net/manual/en/book.json.php