(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_encode — 对变量进行 JSON 编码
编码成功则返回一个以 JSON 形式表示的 string 或者在失败时返回 FALSE
。
版本 | 说明 |
---|---|
5.4.0 |
JSON_PRETTY_PRINT , JSON_UNESCAPED_SLASHES , and JSON_UNESCAPED_UNICODE options were added.
|
5.3.3 |
JSON_NUMERIC_CHECK option was added.
|
5.3.0 |
The options parameter was added.
|
Example #1 A json_encode() 的例子
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
以上例程会输出:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Note:
In the event of a failure to 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, and specified explicitly for each key-value pair.