<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.json-encode.php',
    1 => 'json_encode',
    2 => 'Returns the JSON representation of a value',
  ),
  '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' => 'en',
    '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">Returns the JSON representation of a value</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">
   Returns a string containing the JSON representation of the supplied
   <code class="parameter">value</code>.  If the parameter is an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or <span class="type"><a href="language.types.object.php" class="type object">object</a></span>,
   it will be serialized recursively.
  </p>
  <p class="para">
   If a value to be serialized is an object, then by default only publicly visible
   properties will be included. Alternatively, a class may implement <span class="interfacename"><a href="class.jsonserializable.php" class="interfacename">JsonSerializable</a></span>
   to control how its values are serialized to <abbr title="JavaScript Object Notation">JSON</abbr>.
  </p>
  <p class="para">
   The encoding is affected by the supplied <code class="parameter">flags</code>
   and additionally the encoding of float values depends on the value of
   <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">
       The <code class="parameter">value</code> being encoded. Can be any type except
       a <a href="language.types.resource.php" class="link">resource</a>.
      </p>
      <p class="para">
       All string data must be UTF-8 encoded.
      </p>
      
 <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
  <p class="para">
   PHP реалізує повний набір JSON, як визначено в стандарті <a href="https://datatracker.ietf.org/doc/html/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a>.
  </p>
 </p></blockquote>

     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Bitmask consisting of
       <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>.
       The behaviour of these constants is described on the
       <a href="json.constants.php" class="link">JSON constants</a> page.
      </p>
     </dd>
    
    
     <dt><code class="parameter">depth</code></dt>
     <dd>
      <p class="para">
       Set the maximum depth. Must be greater than zero.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-encode-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns a JSON encoded <span class="type"><a href="language.types.string.php" class="type string">string</a></span> on success  або <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>
        <strong><code><a href="json.constants.php#constant.json-throw-on-error">JSON_THROW_ON_ERROR</a></code></strong>
        <code class="parameter">flags</code> was added.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong>, and
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-substitute">JSON_INVALID_UTF8_SUBSTITUTE</a></code></strong>
        <code class="parameter">flags</code> were added.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <strong><code><a href="json.constants.php#constant.json-unescaped-line-terminators">JSON_UNESCAPED_LINE_TERMINATORS</a></code></strong>
        <code class="parameter">flags</code> was added.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <a href="ini.core.php#ini.serialize-precision" class="link">serialize_precision</a> is
        used instead of <a href="ini.core.php#ini.precision" class="link">precision</a> when
        encoding <span class="type"><a href="language.types.float.php" class="type float">float</a></span> values.
       </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 A <span class="function"><strong>json_encode()</strong></span> example</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 
     A <span class="function"><strong>json_encode()</strong></span> example showing some flags in use
    </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;é&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-3">
    <p><strong>Приклад #3 JSON_NUMERIC_CHECK option example</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 Sequential versus non-sequential array example</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> option example</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">
    In the event of a failure to encode, <span class="function"><a href="function.json-last-error.php" class="function">json_last_error()</a></span>
    can be used to determine the exact nature of the error.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    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.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="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.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> or <span class="type"><a href="language.types.boolean.php" class="type bool">bool</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">
    To summarise, always test that your JSON decoder can handle the output you
    generate from <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><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> - Decodes a JSON string</span></li>
    <li><span class="function"><a href="function.json-last-error.php" class="function" rel="rdfs-seeAlso">json_last_error()</a> - Returns the last error occurred</span></li>
    <li><span class="function"><a href="function.json-last-error-msg.php" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - Returns the error string of the last json_validate(), json_encode() or json_decode() call</span></li>
    <li><span class="function"><a href="function.serialize.php" class="function" rel="rdfs-seeAlso">serialize()</a> - Generates a storable representation of a value</span></li>
   </ul>
  </p>
 </div>

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