JSON stands for JavaScript Object Notation. It consists of a series of key/value pairs. JSON is used to transfer the data between the server and the browser.
PHP Array will also be converted into JSON. With the help of PHP json_encode() function.
PHP json_encode()
PHP json_encode() is a built-in function that converts a PHP value to JSON value. Objects in PHP can be converted into JSON by using a function called json_encode(). The json_encode() function returns the string, if the function works.
Syntax -
json_encode(value, options)
value - Required. Specifies the value to be encoded
options - Optional. Specifies a bitmask (JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR)
Return value - Returns a JSON encoded string on success. FALSE on failure
Example 1-
<?php
$fruit= array("Mango", "Banana", "Apple");
echo json_encode($fruit);
?>
Output -
["Mango","Banana","Apple"]
Example 2-
<?php
$arr = array('Front' => 'Page',
'Fruit' => 'Apple',
'Tree' => 'Mango',
'Developer' => 'PHP');
echo json_encode($arr);
?>
Output -
["Front":"Page","Fruit":"Apple","Tree":"Mango","Developer":"PHP"]
1 comment:
Excellent article and this helps to enhance your knowledge regarding new things. Waiting for more updates.
Use Of PHP In Web Development
PHP Website Development
Post a Comment