Image

9

PHP’s JSON Functions

PHP, a recursive acronym for PHP: Hypertext Preprocessor, is a very popular programming language that is the core of up to 80 percent of the Internet and has its own JSON functions. The question for developers is how to take advantage of what PHP offers for JSON and how best to use it with MySQL. PHP’s JSON functions require UTF8MB4-encoded strings.


JSON_DECODE

The JSON_DECODE function is used to convert a JSON string into a PHP variable. The MySQL JSON functions have no analog.

Image

The first argument is the JSON-formatted string to be decoded. The second is a Boolean value (true or false) to set the returned data into an associative array. The third argument is a recursive depth limit set to 1 or greater. The fourth and final argument has two settable options: JSON_OBJECT_AS_ARRAY, which has the same effect as setting $assoc to true, and JSON_BIGINT_AS_STRING, which casts big integers to strings instead of the default floats.

Example 9-1 Simple PHP program to explore the PHP function JSON_DECODE


Image

Example 9-2 Output of sample PHP program from Example 9-1


Image

Image


JSON_ENCODE

The JSON_ENCODE function turns values of variables into JSON strings:

Image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.108.119