<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.json-encode.php',
    1 => 'json_encode',
    2 => 'Liefert die JSON-Darstellung eines Wertes',
  ),
  'up' => 
  array (
    0 => 'ref.json.php',
    1 => 'JSON-Funktionen',
  ),
  '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' => 'de',
    '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">Liefert die JSON-Darstellung eines Wertes</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.json-encode-description">
  <h3 class="title">Beschreibung</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">
   Gibt eine Zeichenkette zurück, die die JSON-Darstellung des übergebenen
   <code class="parameter">value</code> beinhaltet. Wenn der Parameter ein <a href="language.types.array.php" class="link">Array</a>
   oder <a href="language.types.object.php" class="link">Objekt</a> ist, wird er rekursiv serialisiert.
  </p>
  <p class="para">
   Wenn ein Wert, der serialisiert werden soll, ein Objekt ist, dann werden
   standardmäßig nur die öffentlich sichtbaren Eigenschaften einbezogen.
   Alternativ dazu kann eine Klasse
   <span class="interfacename"><a href="class.jsonserializable.php" class="interfacename">JsonSerializable</a></span> implementieren, um zu
   steuern, wie ihre Werte zu <abbr title="JavaScript Object Notation">JSON</abbr> serialisiert werden.
  </p>
  <p class="para">
   Die Kodierung wird von den übergebenen <code class="parameter">flags</code>
   beeinflusst und zusätzlich hängt die Kodierung von Float-Werten vom Wert
   von <a href="ini.core.php#ini.serialize-precision" class="link">serialize_precision</a> ab.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.json-encode-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       Der zu kodierende <code class="parameter">value</code>. Kann von jedem Typ
       außer <a href="language.types.resource.php" class="link">Ressource</a> sein.
      </p>
      <p class="para">
       Alle Zeichenketten müssen in UTF-8 kodiert sein.
      </p>
      
 <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
  <p class="para">
   PHP implementiert eine Obermenge von JSON wie im Original
   <a href="https://datatracker.ietf.org/doc/html/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a> beschrieben.
  </p>
 </p></blockquote>

     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Eine Bitmaske bestehend aus
       <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>. Das Verhalten dieser
       Konstanten ist auf der Seite über die
       <a href="json.constants.php" class="link">JSON-Konstanten</a> beschrieben.
      </p>
     </dd>
    
    
     <dt><code class="parameter">depth</code></dt>
     <dd>
      <p class="para">
       Setzt die maximale Verschachtelungstiefe. Muss größer als Null sein.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-encode-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Gibt einen JSON-kodierten <a href="language.types.string.php" class="link">String</a> zurück. Bei einem Fehler wird <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurückgegeben.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.json-encode-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Beschreibung</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.3.0</td>
       <td>
        Die <code class="parameter">flags</code>-Konstante
        <strong><code><a href="json.constants.php#constant.json-throw-on-error">JSON_THROW_ON_ERROR</a></code></strong> wurde hinzugefügt.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        Die <code class="parameter">flags</code>-Konstanten
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong> und
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-substitute">JSON_INVALID_UTF8_SUBSTITUTE</a></code></strong> wurden hinzugefügt.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        Die <code class="parameter">flags</code>-Konstante
        <strong><code><a href="json.constants.php#constant.json-unescaped-line-terminators">JSON_UNESCAPED_LINE_TERMINATORS</a></code></strong> wurde
        hinzugefügt.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        <a href="ini.core.php#ini.serialize-precision" class="link">serialize_precision</a>
        wird nun anstatt <a href="ini.core.php#ini.precision" class="link">precision</a>
        verwendet, wenn Werte vom Typ <span class="type"><a href="language.types.float.php" class="type float">float</a></span> (Gleitkommazahlen)
        kodiert werden.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.json-encode-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Ein <span class="function"><strong>json_encode()</strong></span>-Beispiel</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</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>Beispiel #2 
     Ein <span class="function"><strong>json_encode()</strong></span>-Beispiel, das den Einsatz einiger
     Optionen zeigt
    </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">"Ausgabe eines leeren Arrays als 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">"Ausgabe eines leeren Arrays als Objekt: "</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">"Ausgabe eines nichtassoziativen Arrays als 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">"Ausgabe eines nichtassoziativen Arrays als Objekt: "</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">"Assoziative Arrays werden immer als Objekt ausgegeben: "</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">"Assoziative Arrays werden immer als Objekt ausgegeben: "</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</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;]

Ausgabe eines leeren Arrays als Array: []
Ausgabe eines leeren Arrays als Objekt: {}

Ausgabe eines nichtassoziativen Arrays als Array: [[1,2,3]]
Ausgabe eines nichtassoziativen Arrays als Objekt: {&quot;0&quot;:{&quot;0&quot;:1,&quot;1&quot;:2,&quot;2&quot;:3}}

Assoziative Arrays werden immer als Objekt ausgegeben: {&quot;foo&quot;:&quot;bar&quot;,&quot;baz&quot;:&quot;long&quot;}
Assoziative Arrays werden immer als Objekt ausgegeben: {&quot;foo&quot;:&quot;bar&quot;,&quot;baz&quot;:&quot;long&quot;}</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Beispiel #3 JSON_NUMERIC_CHECK-Option-Beispiel</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">"Zeichenketten, die Zahlen darstellen, werden automatisch in Zahlen umgewandelt"</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">"Zeichenketten, die unsachgemäß formatierte Zahlen enthalten"</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>Das oben gezeigte Beispiel erzeugt
eine ähnliche Ausgabe wie:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Zeichenketten, die Zahlen darstellen, werden automatisch in Zahlen umgewandelt
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;
Zeichenketten, die unsachgemäß formatierte Zahlen enthalten
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>Beispiel #4 Beispiel für sequentielle und nicht sequentielle Arrays</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">"Sequentielles 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">"Nicht-sequentielles 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">"Sequentielles Array mit einem entfernten Schlüssel"</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Sequentielles 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;

Nicht-sequentielles 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;

Sequentielles Array mit einem entfernten Schlüssel
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>Beispiel #5 <strong><code><a href="json.constants.php#constant.json-preserve-zero-fraction">JSON_PRESERVE_ZERO_FRACTION</a></code></strong>-Option-Beispiel</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</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">Anmerkungen</h3>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Falls ein Fehler beim kodieren auftritt, kann
    <span class="function"><a href="function.json-last-error.php" class="function">json_last_error()</a></span> verwendet werden, um die genaue
    Ursache des Fehlers festzustellen.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Wenn ein Array kodiert wird und die Schlüssel keine kontinuierliche
    numerische Folge, beginnend bei 0, sind, werden alle Schlüssel als
    Zeichenketten kodiert und explizit für jedes Schlüssel-Wert-Paar
    angegeben.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Wie der Referenz JSON-Encoder gibt auch <span class="function"><strong>json_encode()</strong></span>
    einen einfachen Wert (also weder ein Objekt noch ein Array) aus, wenn ein
    <a href="language.types.string.php" class="link">String</a>, <a href="language.types.integer.php" class="link">Integer</a>, <a href="language.types.float.php" class="link">Float</a> oder <a href="language.types.boolean.php" class="link">Boolean</a> als Eingabe für
    <code class="parameter">value</code> übergeben wird. Während die meisten Decoder
    diese Werte als gültiges JSON akzeptieren, könnte es einige geben die dies
    ablehnen, da die Spezifikationen in diesem Punkt mehrdeutig sind.
   </p>
   <p class="para">
    Um es zusammenzufassen: Prüfen Sie immer, ob ihr JSON-Decoder die Ausgabe,
    die Sie mittels <span class="function"><strong>json_encode()</strong></span> erzeugen, verarbeiten
    kann.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.json-encode-seealso">
  <h3 class="title">Siehe auch</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> - Dekodiert eine JSON-Zeichenkette</span></li>
    <li><span class="function"><a href="function.json-last-error.php" class="function" rel="rdfs-seeAlso">json_last_error()</a> - Gibt den letzten aufgetretenen Fehler zur&uuml;ck</span></li>
    <li><span class="function"><a href="function.json-last-error-msg.php" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - Liefert die Fehlermeldung des letzten Aufrufs von json_validate(),
   json_encode() oder json_decode()</span></li>
    <li><span class="function"><a href="function.serialize.php" class="function" rel="rdfs-seeAlso">serialize()</a> - Erzeugt eine speicherbare Repr&auml;sentation eines Wertes</span></li>
   </ul>
  </p>
 </div>


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