ENCODE JSON IN PHP

(obatambeienwasirherbal.com 5 >= 5.2.0, obatambeienwasirherbal.com. 7, obatambeienwasirherbal.com.. 8, PECL json >= 1.2.0)

json_encode — Returns the JSON representation of a value


(obatambeienwasirherbal.comhường 5 >= 5.2.0, obatambeienwasirherbal.comhường. 7, obatambeienwasirherbal.com. 8, PECL json >= 1.2.0)

json_encode — Returns the JSON representation of a value


json_encode ( mixed $value , int $flags = 0 , int $depth = 512 ) : string |false

Returns a string containing the JSON representation of the supplied value.

The encoding is affected by the supplied flags và additionally the encoding of float values depends on the value of serialize_precision.


Parameters

value The value being encoded. Can be any type except a resource.

All string data must be UTF-8 encoded.

Note:

obatambeienwasirherbal.comhường. implements a superset of JSON as specified in the original » RFC 7159.

flags

Bitmask consisting of 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. The behaviour of these constants is described on the JSON constants page.

depth

Set the maximum depth. Must be greater than zero.


Return Values

Returns a JSON encoded string on success or false on failure.


Changelog

Version Description 7.3.0 JSON_THROW_ON_ERROR flags was added. 7.2.0 JSON_INVALID_UTF8_IGNORE, & JSON_INVALID_UTF8_SUBSTITUTE flags were added. 7.1.0 JSON_UNESCAPED_LINE_TERMINATORS flags was added. 7.1.0 serialize_precision is used instead of precision when encoding double values.


Examples


Example #1 A json_encode() example


$arr = array("a" => 1"b" => 2"c" => 3"d" => 4"e" => 5);

echo 
json_encode($arr);
?>

The above sầu example will output:


Example #2 A json_encode() example showing some flags in use


$a = array("",""bar"",""baz"","&blong&""xc3xa9");

echo 
"Normal: ",  json_encode($a), " ";
echo 
"Tags: ",    json_encode($aJSON_HEX_TAG), " ";
echo 
"Apos: ",    json_encode($aJSON_HEX_APOS), " ";
echo 
"Quot: ",    json_encode($aJSON_HEX_QUOT), " ";
echo 
"Amp: ",     json_encode($aJSON_HEX_AMP), " ";
echo 
"Unicode: "json_encode($aJSON_UNESCAPED_UNICODE), " ";
echo 
"All: ",     json_encode($aJSON_HEX_TAG JSON_HEX_APOS JSON_HEX_QUOT JSON_HEX_AMP JSON_UNESCAPED_UNICODE), " ";

$b = array();

echo 
"Empty array output as array: "json_encode($b), " ";
echo 
"Empty array output as object: "json_encode($bJSON_FORCE_OBJECT), " ";

$c = array(array(1,2,3));

echo 
"Non-associative array output as array: "json_encode($c), " ";
echo 
"Non-associative array output as object: "json_encode($cJSON_FORCE_OBJECT), " ";

$d = array("foo" => "bar""baz" => "long");

echo 
"Associative array always output as object: "json_encode($d), " ";
echo 
"Associative array always output as object: "json_encode($dJSON_FORCE_OBJECT), " ";
?>

The above sầu example will output:


Normal: <"","'bar'",""baz"","&blong&","u00e9"> Tags: <"u003Cfoou003E","'bar'",""baz"","&blong&","u00e9"> Apos: <"","u0027baru0027",""baz"","&blong&","u00e9"> Quot: <"","'bar'","u0022bazu0022","&blong&","u00e9"> Amp: <"","'bar'",""baz"","u0026blongu0026","u00e9"> Unicode: <"","'bar'",""baz"","&blong&","é"> All: <"u003Cfoou003E","u0027baru0027","u0022bazu0022","u0026blongu0026","é"> Empty array output as array: <> Empty array output as object: } Associative array always output as object: Associative sầu array always output as object:

Example #3 JSON_NUMERIC_CHECK option example


echo "Strings representing numbers automatically turned into numbers".

Bạn đang xem: Encode json in php

obatambeienwasirherbal.com_EOL;
$numbers = array("+123123""-123123""1.2e3""0.00001");
var_dump(
 
$numbers,
 
json_encode($numbersJSON_NUMERIC_CHECK)
);
echo 
"Strings containing improperly formatted numbers".obatambeienwasirherbal.com_EOL;
$strings = array("+a33123456789""a123");
var_dump(
 
$strings,
 
json_encode($stringsJSON_NUMERIC_CHECK)
);
?>

The above example will output something similar to:


Strings representing numbers automatically turned inkhổng lồ numbers array(4) string(28) "<123123,-123123,1200,1.0e-5>" Strings containing improperly formatted numbers array(2) string(24) "<"+a33123456789","a123">"

Example #4 Sequential versus non-sequential array example


echo "Sequential array".obatambeienwasirherbal.com_EOL;
$sequential = array("foo""bar""baz""blong");
var_dump(
 
$sequential,
 
json_encode($sequential)
);

echo 
obatambeienwasirherbal.com_EOL.

Xem thêm: Top 11 Php Frameworks For Expert Web Developers 2018 ? Top 5 Must Have Php Frameworks In 2018

"Non-sequential array".obatambeienwasirherbal.com_EOL;
$nonsequential = array(1=>"foo"2=>"bar"3=>"baz"4=>"blong");
var_dump(
 
$nonsequential,
 
json_encode($nonsequential)
);

echo 
obatambeienwasirherbal.com_EOL."Sequential array with one key unset".obatambeienwasirherbal.com_EOL;
unset(
$sequential<1>);
var_dump(
 
$sequential,
 
json_encode($sequential)
);
?>

The above example will output:


Sequential array array(4) string(27) "<"foo","bar","baz","blong">" Non-sequential array array(4) string(43) "" Sequential array with one key unmix array(3) string(33) ""

Example #5 JSON_PRESERVE_ZERO_FRACTION option example


var_dump(json_encode(12.0JSON_PRESERVE_ZERO_FRACTION));
var_dump(json_encode(12.0));
?>

The above example will output:


string(4) "12.0" string(2) "12"

Notes

Note:

In the sự kiện of a failure khổng lồ encode, json_last_error() can be used to determine the exact nature of the error.

Note:

When encoding an array, if the keys are not a continuous numeric sequence starting from 0, all keys are encoded as strings, & specified explicitly for each key-value pair.

Note:

Like the reference JSON encoder, json_encode() will generate JSON that is a simple value (that is, neither an object nor an array) if given a string , int , float or bool as an đầu vào value. While most decoders will accept these values as valid JSON, some may not, as the specification is ambiguous on this point.

To summarise, always test that your JSON decoder can handle the output you generate from json_encode() .


See Also

JsonSerializable json_decode() - Decodes a JSON string json_last_error() - Returns the last error occurred serialize() - Generates a storable representation of a value