mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-19 18:38:55 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@310659 c90b9560-bf6c-de11-be94-00142212c4b1
208 lines
5.5 KiB
XML
208 lines
5.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<refentry xml:id="function.json-encode" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>json_encode</refname>
|
|
<refpurpose>Returns the JSON representation of a value</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>string</type><methodname>json_encode</methodname>
|
|
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Returns a string containing the JSON representation of
|
|
<parameter>value</parameter>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>value</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The <parameter>value</parameter> being encoded. Can be any type except
|
|
a <type>resource</type>.
|
|
</para>
|
|
<para>
|
|
This function only works with UTF-8 encoded data.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>options</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Bitmask consisting of <constant>JSON_HEX_QUOT</constant>,
|
|
<constant>JSON_HEX_TAG</constant>,
|
|
<constant>JSON_HEX_AMP</constant>,
|
|
<constant>JSON_HEX_APOS</constant>,
|
|
<constant>JSON_FORCE_OBJECT</constant>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Returns a JSON encoded <type>string</type> on success.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>5.3.0</entry>
|
|
<entry>
|
|
The <parameter>options</parameter> parameter was added.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.2.1</entry>
|
|
<entry>
|
|
Added support for JSON encoding of basic types.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>A <function>json_encode</function> example</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
|
|
|
|
echo json_encode($arr);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
{"a":1,"b":2,"c":3,"d":4,"e":5}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
<example>
|
|
<title>A <function>json_encode</function> example showing all the options in action</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = array('<foo>',"'bar'",'"baz"','&blong&');
|
|
|
|
echo "Normal: ", json_encode($a), "\n";
|
|
echo "Tags: ", json_encode($a,JSON_HEX_TAG), "\n";
|
|
echo "Apos: ", json_encode($a,JSON_HEX_APOS), "\n";
|
|
echo "Quot: ", json_encode($a,JSON_HEX_QUOT), "\n";
|
|
echo "Amp: ", json_encode($a,JSON_HEX_AMP), "\n";
|
|
echo "All: ", json_encode($a,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP), "\n\n";
|
|
|
|
$b = array();
|
|
|
|
echo "Empty array output as array: ", json_encode($b), "\n";
|
|
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
|
|
|
|
$c = array(array(1,2,3));
|
|
|
|
echo "Non-associative array output as array: ", json_encode($c), "\n";
|
|
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
|
|
|
|
$d = array('foo' => 'bar', 'baz' => 'long');
|
|
|
|
echo "Associative array always output as object: ", json_encode($d), "\n";
|
|
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Normal: ["<foo>","'bar'","\"baz\"","&blong&"]
|
|
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&"]
|
|
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&"]
|
|
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&"]
|
|
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026"]
|
|
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026"]
|
|
|
|
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: {"0":{"0":1,"1":2,"2":3}}
|
|
|
|
Associative array always output as object: {"foo":"bar","baz":"long"}
|
|
Associative array always output as object: {"foo":"bar","baz":"long"}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
In the event of a failure to encode, <function>json_last_error</function>
|
|
can be used to determine the exact nature of the error.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>json_decode</function></member>
|
|
<member><function>json_last_error</function></member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|