uTools-Manuals/docs/php/json_encode.html
2019-04-08 23:22:26 +08:00

393 lines
28 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>对变量进行 JSON 编码</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.json-encode" class="refentry">
<div class="refnamediv">
<h1 class="refname">json_encode</h1>
<p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PECL json &gt;= 1.2.0)</p><p class="refpurpose"><span class="refname">json_encode</span> &mdash; <span class="dc-title">对变量进行 JSON 编码</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.json-encode-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>json_encode</strong></span>
( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>
]] ) : <span class="type">string</span></div>
<p class="para rdfs-comment">
返回字符串,包含了 <code class="parameter">value</code> 值 JSON 形式的表示。
</p>
<p class="para">
编码受传入的 <code class="parameter">options</code> 参数影响,此外浮点值的编码依赖于 <a href="ini.core.html#ini.serialize-precision" class="link">serialize_precision</a>
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.json-encode-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">value</code></dt>
<dd>
<p class="para">
待编码的 <code class="parameter">value</code> ,除了<span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>
类型之外,可以为任何数据类型。
</p>
<p class="para">
所有字符串数据的编码必须是 UTF-8。
</p>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
PHP implements a superset of JSON as specified in the original
<a href="http://www.faqs.org/rfcs/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a>.
</p>
</p></blockquote>
</dd>
<dt>
<code class="parameter">options</code></dt>
<dd>
<p class="para">
由以下常量组成的二进制掩码: <strong><code>JSON_HEX_QUOT</code></strong>,
<strong><code>JSON_HEX_TAG</code></strong>,
<strong><code>JSON_HEX_AMP</code></strong>,
<strong><code>JSON_HEX_APOS</code></strong>,
<strong><code>JSON_NUMERIC_CHECK</code></strong>,
<strong><code>JSON_PRETTY_PRINT</code></strong>,
<strong><code>JSON_UNESCAPED_SLASHES</code></strong>,
<strong><code>JSON_FORCE_OBJECT</code></strong>,
<strong><code>JSON_PRESERVE_ZERO_FRACTION</code></strong>,
<strong><code>JSON_UNESCAPED_UNICODE</code></strong>,
<strong><code>JSON_PARTIAL_OUTPUT_ON_ERROR</code></strong>
关于 JSON 常量详情参考<a href="json.constants.html" class="link">JSON 常量</a>页面。
</p>
</dd>
<dt>
<code class="parameter">depth</code></dt>
<dd>
<p class="para">
设置最大深度。 必须大于0。
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.json-encode-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
成功则返回 JSON 编码的 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 或者在失败时返回 <strong><code>FALSE</code></strong>
</p>
</div>
<div class="refsect1 changelog" id="refsect1-function.json-encode-changelog">
<h3 class="title">更新日志</h3>
<p class="para">
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>7.1.0</td>
<td>
对 Double 的值进行编码时,使用 <a href="ini.core.html#ini.serialize-precision" class="link">serialize_precision</a> 代替 <a href="ini.core.html#ini.precision" class="link">precision</a>
</td>
</tr>
<tr>
<td>5.6.6</td>
<td>
<code class="parameter">options</code> 参数增加常量:
<strong><code>JSON_PRESERVE_ZERO_FRACTION</code></strong>
</td>
</tr>
<tr>
<td>5.5.0</td>
<td>
增加 <code class="parameter">depth</code> 参数。
</td>
</tr>
<tr>
<td>5.5.0</td>
<td>
增加了 <strong><code>JSON_PARTIAL_OUTPUT_ON_ERROR</code></strong> 选项。
</td>
</tr>
<tr>
<td>5.5.0</td>
<td>
失败时返回的值从 <em>null</em> 字符串改成 <strong><code>FALSE</code></strong>
</td>
</tr>
<tr>
<td>5.4.0</td>
<td>
<code class="parameter">options</code> 参数增加常量:
<strong><code>JSON_PRETTY_PRINT</code></strong>, <strong><code>JSON_UNESCAPED_SLASHES</code></strong>, 和 <strong><code>JSON_UNESCAPED_UNICODE</code></strong>
</td>
</tr>
<tr>
<td>5.3.3</td>
<td>
<code class="parameter">options</code> 参数增加常量:<strong><code>JSON_NUMERIC_CHECK</code></strong>
</td>
</tr>
<tr>
<td>5.3.0</td>
<td>
增加 <code class="parameter">options</code> 参数.
</td>
</tr>
</tbody>
</table>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.json-encode-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-4518">
<p><strong>Example #1 <span class="function"><strong>json_encode()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">1</span><span style="color: #007700">,</span><span style="color: #DD0000">'b'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">2</span><span style="color: #007700">,</span><span style="color: #DD0000">'c'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">3</span><span style="color: #007700">,</span><span style="color: #DD0000">'d'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">4</span><span style="color: #007700">,</span><span style="color: #DD0000">'e'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
{&quot;a&quot;:1,&quot;b&quot;:2,&quot;c&quot;:3,&quot;d&quot;:4,&quot;e&quot;:5}
</pre></div>
</div>
</div>
<div class="example" id="example-4519">
<p><strong>Example #2 <span class="function"><strong>json_encode()</strong></span> 函数中 <code class="parameter">options</code> 参数的用法</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'&lt;foo&gt;'</span><span style="color: #007700">,</span><span style="color: #DD0000">"'bar'"</span><span style="color: #007700">,</span><span style="color: #DD0000">'"baz"'</span><span style="color: #007700">,</span><span style="color: #DD0000">'&amp;blong&amp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\xc3\xa9"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Normal:&nbsp;"</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Tags:&nbsp;"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_HEX_TAG</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Apos:&nbsp;"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_HEX_APOS</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Quot:&nbsp;"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_HEX_QUOT</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Amp:&nbsp;"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_HEX_AMP</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Unicode:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_UNESCAPED_UNICODE</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"All:&nbsp;"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_HEX_TAG&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">JSON_HEX_APOS&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">JSON_HEX_QUOT&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">JSON_HEX_AMP&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">JSON_UNESCAPED_UNICODE</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Empty&nbsp;array&nbsp;output&nbsp;as&nbsp;array:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Empty&nbsp;array&nbsp;output&nbsp;as&nbsp;object:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_FORCE_OBJECT</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;array(array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,</span><span style="color: #0000BB">2</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Non-associative&nbsp;array&nbsp;output&nbsp;as&nbsp;array:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Non-associative&nbsp;array&nbsp;output&nbsp;as&nbsp;object:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_FORCE_OBJECT</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'long'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Associative&nbsp;array&nbsp;always&nbsp;output&nbsp;as&nbsp;object:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Associative&nbsp;array&nbsp;always&nbsp;output&nbsp;as&nbsp;object:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_FORCE_OBJECT</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Normal: [&quot;&lt;foo&gt;&quot;,&quot;&#039;bar&#039;&quot;,&quot;\&quot;baz\&quot;&quot;,&quot;&amp;blong&amp;&quot;,&quot;\u00e9&quot;]
Tags: [&quot;\u003Cfoo\u003E&quot;,&quot;&#039;bar&#039;&quot;,&quot;\&quot;baz\&quot;&quot;,&quot;&amp;blong&amp;&quot;,&quot;\u00e9&quot;]
Apos: [&quot;&lt;foo&gt;&quot;,&quot;\u0027bar\u0027&quot;,&quot;\&quot;baz\&quot;&quot;,&quot;&amp;blong&amp;&quot;,&quot;\u00e9&quot;]
Quot: [&quot;&lt;foo&gt;&quot;,&quot;&#039;bar&#039;&quot;,&quot;\u0022baz\u0022&quot;,&quot;&amp;blong&amp;&quot;,&quot;\u00e9&quot;]
Amp: [&quot;&lt;foo&gt;&quot;,&quot;&#039;bar&#039;&quot;,&quot;\&quot;baz\&quot;&quot;,&quot;\u0026blong\u0026&quot;,&quot;\u00e9&quot;]
Unicode: [&quot;&lt;foo&gt;&quot;,&quot;&#039;bar&#039;&quot;,&quot;\&quot;baz\&quot;&quot;,&quot;&amp;blong&amp;&quot;,&quot;é&quot;]
All: [&quot;\u003Cfoo\u003E&quot;,&quot;\u0027bar\u0027&quot;,&quot;\u0022baz\u0022&quot;,&quot;\u0026blong\u0026&quot;,&quot;é&quot;]
Empty array output as array: []
Empty array output as object: {}
Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {&quot;0&quot;:{&quot;0&quot;:1,&quot;1&quot;:2,&quot;2&quot;:3}}
Associative array always output as object: {&quot;foo&quot;:&quot;bar&quot;,&quot;baz&quot;:&quot;long&quot;}
Associative array always output as object: {&quot;foo&quot;:&quot;bar&quot;,&quot;baz&quot;:&quot;long&quot;}
</pre></div>
</div>
</div>
<div class="example" id="example-4520">
<p><strong>Example #3 选项 JSON_NUMERIC_CHECK 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Strings&nbsp;representing&nbsp;numbers&nbsp;automatically&nbsp;turned&nbsp;into&nbsp;numbers"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$numbers&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'+123123'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'-123123'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'1.2e3'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0.00001'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />&nbsp;</span><span style="color: #0000BB">$numbers</span><span style="color: #007700">,<br />&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$numbers</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_NUMERIC_CHECK</span><span style="color: #007700">)<br />);<br />echo&nbsp;</span><span style="color: #DD0000">"Strings&nbsp;containing&nbsp;improperly&nbsp;formatted&nbsp;numbers"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$strings&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'+a33123456789'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a123'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />&nbsp;</span><span style="color: #0000BB">$strings</span><span style="color: #007700">,<br />&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$strings</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_NUMERIC_CHECK</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程的输出类似于:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Strings representing numbers automatically turned into numbers
array(4) {
[0]=&gt;
string(7) &quot;+123123&quot;
[1]=&gt;
string(7) &quot;-123123&quot;
[2]=&gt;
string(5) &quot;1.2e3&quot;
[3]=&gt;
string(7) &quot;0.00001&quot;
}
string(28) &quot;[123123,-123123,1200,1.0e-5]&quot;
Strings containing improperly formatted numbers
array(2) {
[0]=&gt;
string(13) &quot;+a33123456789&quot;
[1]=&gt;
string(4) &quot;a123&quot;
}
string(24) &quot;[&quot;+a33123456789&quot;,&quot;a123&quot;]&quot;
</pre></div>
</div>
</div>
<div class="example" id="example-4521">
<p><strong>Example #4 连续与非连续数组示例</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"连续数组"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sequential&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"baz"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blong"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />&nbsp;</span><span style="color: #0000BB">$sequential</span><span style="color: #007700">,<br />&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$sequential</span><span style="color: #007700">)<br />);<br /><br />echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">.</span><span style="color: #DD0000">"非连续数组"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$nonsequential&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"baz"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"blong"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />&nbsp;</span><span style="color: #0000BB">$nonsequential</span><span style="color: #007700">,<br />&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$nonsequential</span><span style="color: #007700">)<br />);<br /><br />echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">.</span><span style="color: #DD0000">"删除一个连续数组值的方式产生的非连续数组"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />unset(</span><span style="color: #0000BB">$sequential</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br />&nbsp;</span><span style="color: #0000BB">$sequential</span><span style="color: #007700">,<br />&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$sequential</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
连续数组
array(4) {
[0]=&gt;
string(3) &quot;foo&quot;
[1]=&gt;
string(3) &quot;bar&quot;
[2]=&gt;
string(3) &quot;baz&quot;
[3]=&gt;
string(5) &quot;blong&quot;
}
string(27) &quot;[&quot;foo&quot;,&quot;bar&quot;,&quot;baz&quot;,&quot;blong&quot;]&quot;
非连续数组
array(4) {
[1]=&gt;
string(3) &quot;foo&quot;
[2]=&gt;
string(3) &quot;bar&quot;
[3]=&gt;
string(3) &quot;baz&quot;
[4]=&gt;
string(5) &quot;blong&quot;
}
string(43) &quot;{&quot;1&quot;:&quot;foo&quot;,&quot;2&quot;:&quot;bar&quot;,&quot;3&quot;:&quot;baz&quot;,&quot;4&quot;:&quot;blong&quot;}&quot;
删除一个连续数组值的方式产生的非连续数组
array(3) {
[0]=&gt;
string(3) &quot;foo&quot;
[2]=&gt;
string(3) &quot;baz&quot;
[3]=&gt;
string(5) &quot;blong&quot;
}
string(33) &quot;{&quot;0&quot;:&quot;foo&quot;,&quot;2&quot;:&quot;baz&quot;,&quot;3&quot;:&quot;blong&quot;}&quot;
</pre></div>
</div>
</div>
<div class="example" id="example-4522">
<p><strong>Example #5 <strong><code>选项 JSON_PRESERVE_ZERO_FRACTION</code></strong> 的例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">12.0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_PRESERVE_ZERO_FRACTION</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">12.0</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
string(4) &quot;12.0&quot;
string(2) &quot;12&quot;
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 notes" id="refsect1-function.json-encode-notes">
<h3 class="title">注释</h3>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
如果执行失败,可以通过 <span class="function"><a href="json_last_error.html" class="function">json_last_error()</a></span> 函数来获取详细错误信息。
</p>
</p></blockquote>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
如果要编码的数组的键不是从0开始的数字所有的键将会被当作字符串并明确声明为 key-value 对。
</p>
</p></blockquote>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
Like the reference JSON encoder, <span class="function"><strong>json_encode()</strong></span> will
generate JSON that is a simple value (that is, neither an object nor an
array) if given a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>,
<span class="type"><a href="language.types.float.html" class="type float">float</a></span> or <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> as an input
<code class="parameter">value</code>. While most decoders will accept these values
as valid JSON, some may not, as the specification is ambiguous on this
point.
</p>
<p class="para">
总而言之,应该测试下 JSON decoder 能否处理 <span class="function"><strong>json_encode()</strong></span> 生成的数据。
</p>
</p></blockquote>
</div>
<div class="refsect1 seealso" id="refsect1-function.json-encode-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="interfacename"><a href="class.jsonserializable.html" class="interfacename">JsonSerializable</a></span></li>
<li class="member"><span class="function"><a href="json_decode.html" class="function" rel="rdfs-seeAlso">json_decode()</a> - 对 JSON 格式的字符串进行解码</span></li>
<li class="member"><span class="function"><a href="json_last_error.html" class="function" rel="rdfs-seeAlso">json_last_error()</a> - 返回最后发生的错误</span></li>
<li class="member"><span class="function"><a href="serialize.html" class="function" rel="rdfs-seeAlso">serialize()</a> - 产生一个可存储的值的表示</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>