<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.json.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.json-encode.php',
    1 => 'json_encode',
    2 => '値を JSON 形式にして返す',
  ),
  'up' => 
  array (
    0 => 'ref.json.php',
    1 => 'JSON 関数',
  ),
  'prev' => 
  array (
    0 => 'function.json-decode.php',
    1 => 'json_decode',
  ),
  'next' => 
  array (
    0 => 'function.json-last-error.php',
    1 => 'json_last_error',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/json/functions/json-encode.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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, PHP 8, 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.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   与えられた <code class="parameter">value</code> を JSON 形式にした文字列を返します。
   引数が配列やオブジェクトの場合は、JSON 形式に再帰的に変換されます。
  </p>
  <p class="para">
   変換される値がオブジェクトの場合、
   デフォルトでは public としてアクセス可能なプロパティのみが含まれます。
   それ以外のやり方で、値を <abbr title="JavaScript Object Notation">JSON</abbr> 形式に変換する方法は、
   <span class="interfacename"><a href="class.jsonserializable.php" class="interfacename">JsonSerializable</a></span>
   を実装することで制御できます。
  </p>
  <p class="para">
   エンコーディングは、<code class="parameter">flags</code> の指定によって影響を受けます。
   さらに、浮動小数点のエンコーディングは、
   <a href="ini.core.php#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">
       エンコードする値。
       <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> 型以外の任意の型を指定できます。
      </p>
      <p class="para">
       すべての文字列データは、UTF-8 エンコードされていなければいけません。
      </p>
      
 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   PHP の実装は、
   <a href="https://datatracker.ietf.org/doc/html/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a> の JSON のスーパーセットです。
  </p>
 </p></blockquote>

     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <strong><code><a href="json.constants.php#constant.json-force-object">JSON_FORCE_OBJECT</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-hex-quot">JSON_HEX_QUOT</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-hex-tag">JSON_HEX_TAG</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-hex-amp">JSON_HEX_AMP</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-hex-apos">JSON_HEX_APOS</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-invalid-utf8-substitute">JSON_INVALID_UTF8_SUBSTITUTE</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-numeric-check">JSON_NUMERIC_CHECK</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-partial-output-on-error">JSON_PARTIAL_OUTPUT_ON_ERROR</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-preserve-zero-fraction">JSON_PRESERVE_ZERO_FRACTION</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-pretty-print">JSON_PRETTY_PRINT</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-unescaped-line-terminators">JSON_UNESCAPED_LINE_TERMINATORS</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-unescaped-slashes">JSON_UNESCAPED_SLASHES</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-unescaped-unicode">JSON_UNESCAPED_UNICODE</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-throw-on-error">JSON_THROW_ON_ERROR</a></code></strong>
       からなるビットマスク。
       各定数の意味については
       <a href="json.constants.php" class="link">JSON 定数のページ</a>
       に説明があります。
      </p>
     </dd>
    
    
     <dt><code class="parameter">depth</code></dt>
     <dd>
      <p class="para">
       最大の深さを設定します。正の数でなければいけません。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-encode-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   成功した場合に、JSON エンコードされた文字列を返します。
   失敗した場合に <strong><code><a href="reserved.constants.php#constant.false">false</a></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.3.0</td>
       <td>
        <code class="parameter">flags</code> パラメータに 
        <strong><code><a href="json.constants.php#constant.json-throw-on-error">JSON_THROW_ON_ERROR</a></code></strong> が追加されました。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <code class="parameter">flags</code> パラメータに 
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong> と
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-substitute">JSON_INVALID_UTF8_SUBSTITUTE</a></code></strong> が追加されました。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <code class="parameter">flags</code> パラメータに 
        <strong><code><a href="json.constants.php#constant.json-unescaped-line-terminators">JSON_UNESCAPED_LINE_TERMINATORS</a></code></strong> が追加されました。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <span class="type"><a href="language.types.float.php" class="type float">float</a></span> 値をエンコードする際に、
        <a href="ini.core.php#ini.precision" class="link">precision</a> ではなく
        <a href="ini.core.php#ini.serialize-precision" class="link">serialize_precision</a> を使うようになりました。
       </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-1">
    <p><strong>例1 <span class="function"><strong>json_encode()</strong></span> の例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$arr </span><span style="color: #007700">= array(</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 </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></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">{&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-2">
    <p><strong>例2 
     <span class="function"><strong>json_encode()</strong></span> で、利用中のいくつかのフラグを表示する例
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= 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">, </span><span style="color: #DD0000">"\xc3\xa9"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Normal: "</span><span style="color: #007700">,  </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Tags: "</span><span style="color: #007700">,    </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_HEX_TAG</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Apos: "</span><span style="color: #007700">,    </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_HEX_APOS</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Quot: "</span><span style="color: #007700">,    </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_HEX_QUOT</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Amp: "</span><span style="color: #007700">,     </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_HEX_AMP</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Unicode: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_UNESCAPED_UNICODE</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"All: "</span><span style="color: #007700">,     </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_HEX_TAG </span><span style="color: #007700">| </span><span style="color: #0000BB">JSON_HEX_APOS </span><span style="color: #007700">| </span><span style="color: #0000BB">JSON_HEX_QUOT </span><span style="color: #007700">| </span><span style="color: #0000BB">JSON_HEX_AMP </span><span style="color: #007700">| </span><span style="color: #0000BB">JSON_UNESCAPED_UNICODE</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array();<br /><br />echo </span><span style="color: #DD0000">"Empty array output as array: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Empty array output as object: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_FORCE_OBJECT</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= 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 </span><span style="color: #DD0000">"Non-associative array output as array: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Non-associative array output as object: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_FORCE_OBJECT</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'long'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Associative array always output as object: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Associative array always output as object: "</span><span style="color: #007700">, </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_FORCE_OBJECT</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">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;e&quot;]
All: [&quot;\u003Cfoo\u003E&quot;,&quot;\u0027bar\u0027&quot;,&quot;\u0022baz\u0022&quot;,&quot;\u0026blong\u0026&quot;,&quot;e&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-3">
    <p><strong>例3 JSON_NUMERIC_CHECK の例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Strings representing numbers automatically turned into 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 </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'+123123'</span><span style="color: #007700">, </span><span style="color: #DD0000">'-123123'</span><span style="color: #007700">, </span><span style="color: #DD0000">'1.2e3'</span><span style="color: #007700">, </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 /> </span><span style="color: #0000BB">$numbers</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$numbers</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_NUMERIC_CHECK</span><span style="color: #007700">)<br />);<br />echo </span><span style="color: #DD0000">"Strings containing improperly formatted 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 </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'+a33123456789'</span><span style="color: #007700">, </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 /> </span><span style="color: #0000BB">$strings</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$strings</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_NUMERIC_CHECK</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">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-4">
    <p><strong>例4 シーケンシャルな配列とそうでない配列の例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Sequential array"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sequential </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"foo"</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">"blong"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(<br /> </span><span style="color: #0000BB">$sequential</span><span style="color: #007700">,<br /> </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 </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">.</span><span style="color: #DD0000">"Non-sequential array"</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$nonsequential </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"baz"</span><span style="color: #007700">, </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 /> </span><span style="color: #0000BB">$nonsequential</span><span style="color: #007700">,<br /> </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 </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">.</span><span style="color: #DD0000">"Sequential array with one key unset"</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 /> </span><span style="color: #0000BB">$sequential</span><span style="color: #007700">,<br /> </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></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Sequential array
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;

Non-sequential array
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;

Sequential array with one key unset
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-5">
    <p><strong>例5 <strong><code><a href="json.constants.php#constant.json-preserve-zero-fraction">JSON_PRESERVE_ZERO_FRACTION</a></code></strong> の例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><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">, </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></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">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">注意</strong>: 
   <p class="para">
    エンコードに失敗した場合は、<span class="function"><a href="function.json-last-error.php" class="function">json_last_error()</a></span>
    を使ってエラーの内容を調べることができます。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    配列をエンコードする場合、もし配列のキーが 0 からはじまる連続した数値でなければ、
    すべてのキーを文字列としてエンコードします。
    そして、個々のキー/値のペアを明示的に指定します。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    JSON エンコーダーのリファレンス実装と同様、
    <span class="type"><a href="language.types.string.php" class="type string">string</a></span> や <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>、
    <span class="type"><a href="language.types.float.php" class="type float">float</a></span> そして <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> を入力として渡したときに
    <span class="function"><strong>json_encode()</strong></span> が生成する JSON
    はシンプルな値 (オブジェクトでもないし配列でもないもの) となります。
    大半のエンコーダーはこういった値を妥当な JSON として扱いますが、中にはそうでないものもあります。
    そのため、現時点ではこの仕様は不明瞭です。
   </p>
   <p class="para">
    要するに、<span class="function"><strong>json_encode()</strong></span> で生成した JSON
    が、あなたの使おうとしているデコーダーでデコードできるかどうかを常に確認する必要があるということです。
   </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><span class="interfacename"><a href="class.jsonserializable.php" class="interfacename">JsonSerializable</a></span></li>
    <li><span class="function"><a href="function.json-decode.php" class="function" rel="rdfs-seeAlso">json_decode()</a> - JSON 文字列をデコードする</span></li>
    <li><span class="function"><a href="function.json-last-error.php" class="function" rel="rdfs-seeAlso">json_last_error()</a> - 直近に発生したエラーを返す</span></li>
    <li><span class="function"><a href="function.json-last-error-msg.php" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - 直近の json_validate()、json_encode()、json_decode() の呼び出しのエラー文字列を返す</span></li>
    <li><span class="function"><a href="function.serialize.php" class="function" rel="rdfs-seeAlso">serialize()</a> - 値の保存可能な表現を生成する</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>