2010-03-28 22:10:10 +00:00
<?xml version="1.0" encoding="utf-8"?>
2009-07-11 07:50:41 +00:00
<!-- $Revision$ -->
2007-06-20 22:25:43 +00:00
<refentry xml:id= "function.json-encode" xmlns= "http://docbook.org/ns/docbook" >
2006-08-02 15:43:50 +00:00
<refnamediv >
<refname > json_encode</refname>
2008-04-23 17:58:34 +00:00
<refpurpose > Returns the JSON representation of a value</refpurpose>
2006-08-02 15:43:50 +00:00
</refnamediv>
<refsect1 role= "description" >
&reftitle.description;
<methodsynopsis >
<type > string</type> <methodname > json_encode</methodname>
<methodparam > <type > mixed</type> <parameter > value</parameter> </methodparam>
2009-01-12 15:45:03 +00:00
<methodparam choice= "opt" > <type > int</type> <parameter > options</parameter> <initializer > 0</initializer> </methodparam>
2013-05-16 20:12:39 +00:00
<methodparam choice= "opt" > <type > int</type> <parameter > depth</parameter> <initializer > 512</initializer> </methodparam>
2006-08-02 15:43:50 +00:00
</methodsynopsis>
<para >
2008-04-23 17:58:34 +00:00
Returns a string containing the JSON representation of
<parameter > value</parameter> .
2006-08-02 15:43:50 +00:00
</para>
</refsect1>
<refsect1 role= "parameters" >
&reftitle.parameters;
<para >
<variablelist >
<varlistentry >
<term > <parameter > value</parameter> </term>
<listitem >
<para >
2008-04-23 17:58:34 +00:00
The <parameter > value</parameter> being encoded. Can be any type except
a <type > resource</type> .
2006-08-02 15:43:50 +00:00
</para>
2007-01-01 16:03:48 +00:00
<para >
2008-04-23 17:58:34 +00:00
This function only works with UTF-8 encoded data.
2007-01-01 16:03:48 +00:00
</para>
2006-08-02 15:43:50 +00:00
</listitem>
</varlistentry>
2008-11-04 13:27:54 +00:00
<varlistentry >
<term > <parameter > options</parameter> </term>
<listitem >
<para >
2009-03-19 15:42:54 +00:00
Bitmask consisting of <constant > JSON_HEX_QUOT</constant> ,
<constant > JSON_HEX_TAG</constant> ,
<constant > JSON_HEX_AMP</constant> ,
<constant > JSON_HEX_APOS</constant> ,
2011-06-16 12:55:11 +00:00
<constant > JSON_NUMERIC_CHECK</constant> ,
2011-06-30 14:02:46 +00:00
<constant > JSON_PRETTY_PRINT</constant> ,
<constant > JSON_UNESCAPED_SLASHES</constant> ,
2011-08-29 16:22:02 +00:00
<constant > JSON_FORCE_OBJECT</constant> ,
2012-11-06 05:19:26 +00:00
<constant > JSON_UNESCAPED_UNICODE</constant> . The behaviour of these
constants is described on
<link linkend= "json.constants" > the JSON constants page</link> .
2008-11-04 13:27:54 +00:00
</para>
</listitem>
</varlistentry>
2006-08-02 15:43:50 +00:00
</variablelist>
</para>
</refsect1>
<refsect1 role= "returnvalues" >
&reftitle.returnvalues;
<para >
2012-03-19 02:30:00 +00:00
Returns a JSON encoded <type > string</type> on success &return.falseforfailure; .
2006-08-02 15:43:50 +00:00
</para>
</refsect1>
2007-05-25 19:57:31 +00:00
<refsect1 role= "changelog" >
&reftitle.changelog;
<para >
<informaltable >
<tgroup cols= "2" >
<thead >
<row >
<entry > &Version; </entry>
<entry > &Description; </entry>
</row>
</thead>
<tbody >
2013-05-16 20:12:39 +00:00
<row >
<entry > 5.5.0</entry>
<entry >
<parameter > depth</parameter> parameter was added.
</entry>
</row>
2011-07-27 22:17:57 +00:00
<row >
<entry > 5.4.0</entry>
<entry >
2012-03-27 01:11:46 +00:00
<constant > JSON_PRETTY_PRINT</constant> , <constant > JSON_UNESCAPED_SLASHES</constant> , and <constant > JSON_UNESCAPED_UNICODE</constant> <parameter > options</parameter> were added.
2011-08-04 01:16:14 +00:00
</entry>
</row>
<row >
<entry > 5.3.3</entry>
<entry >
<constant > JSON_NUMERIC_CHECK</constant> <parameter > option</parameter> was added.
2011-07-27 22:17:57 +00:00
</entry>
</row>
2007-05-25 19:57:31 +00:00
<row >
2009-06-19 19:06:49 +00:00
<entry > 5.3.0</entry>
2007-05-25 19:57:31 +00:00
<entry >
2009-06-19 19:06:49 +00:00
The <parameter > options</parameter> parameter was added.
2009-03-19 15:42:54 +00:00
</entry>
</row>
2007-05-25 19:57:31 +00:00
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
2006-08-02 15:43:50 +00:00
<refsect1 role= "examples" >
&reftitle.examples;
<para >
<example >
2008-04-23 17:58:34 +00:00
<title > A <function > json_encode</function> example</title>
2006-08-02 15:43:50 +00:00
<programlisting role= "php" >
< ![CDATA[
< ?php
2011-06-16 12:57:06 +00:00
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
2006-08-02 15:43:50 +00:00
echo json_encode($arr);
?>
]]>
</programlisting>
&example.outputs;
<screen >
< ![CDATA[
{"a":1,"b":2,"c":3,"d":4,"e":5}
2009-03-19 15:42:54 +00:00
]]>
</screen>
</example>
<example >
2012-11-06 05:19:26 +00:00
<title >
A <function > json_encode</function> example showing some options in use
</title>
2009-03-19 15:42:54 +00:00
<programlisting role= "php" >
< ![CDATA[
< ?php
2011-08-29 16:22:02 +00:00
$a = array('<foo > ',"'bar'",'"baz"','& blong& ', "\xc3\xa9");
2009-03-19 15:42:54 +00:00
2011-08-29 16:22:02 +00:00
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 "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
2009-03-19 15:42:54 +00:00
$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";
2010-10-20 10:17:23 +00:00
$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";
2009-03-19 15:42:54 +00:00
?>
]]>
</programlisting>
&example.outputs;
<screen >
< ![CDATA[
2011-08-29 16:22:02 +00:00
Normal: ["<foo > ","'bar'","\"baz\"","& blong& ","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","& blong& ","\u00e9"]
Apos: ["<foo > ","\u0027bar\u0027","\"baz\"","& blong& ","\u00e9"]
Quot: ["<foo > ","'bar'","\u0022baz\u0022","& blong& ","\u00e9"]
Amp: ["<foo > ","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["<foo > ","'bar'","\"baz\"","& blong& ","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]
2009-03-19 15:42:54 +00:00
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}}
2010-10-20 10:17:23 +00:00
Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
2011-12-06 23:36:32 +00:00
]]>
</screen>
</example>
<example >
<title > Sequential versus non-sequential array example</title>
<programlisting role= "php" >
< ![CDATA[
< ?php
echo "Sequential array".PHP_EOL;
$sequential = array("foo", "bar", "baz", "blong");
var_dump(
$sequential,
json_encode($sequential)
);
echo PHP_EOL."Non-sequential array".PHP_EOL;
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
var_dump(
$nonsequential,
json_encode($nonsequential)
);
echo PHP_EOL."Sequential array with one key unset".PHP_EOL;
unset($sequential[1]);
var_dump(
$sequential,
json_encode($sequential)
);
?>
]]>
</programlisting>
&example.outputs;
<screen >
< ![CDATA[
Sequential array
array(4) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(27) "["foo","bar","baz","blong"]"
Non-sequential array
array(4) {
[1]=>
string(3) "foo"
[2]=>
string(3) "bar"
[3]=>
string(3) "baz"
[4]=>
string(5) "blong"
}
string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}"
Sequential array with one key unset
array(3) {
[0]=>
string(3) "foo"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(33) "{"0":"foo","2":"baz","3":"blong"}"
2006-08-02 15:43:50 +00:00
]]>
</screen>
</example>
</para>
</refsect1>
2011-04-30 13:13:52 +00:00
<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>
2011-12-06 23:36:32 +00:00
<note >
<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.
</para>
</note>
2012-08-06 01:39:02 +00:00
<note >
<para >
Like the reference JSON encoder, <function > json_encode</function> will
generate JSON that is a simple value (that is, neither an object nor an
array) if given a <type > string</type> , <type > integer</type> ,
<type > float</type> or <type > boolean</type> as an input
<parameter > value</parameter> . While most decoders will accept these values
as valid JSON, some may not, as the specification is ambiguous on this
point.
</para>
<para >
To summarise, always test that your JSON decoder can handle the output you
generate from <function > json_encode</function> .
</para>
</note>
2011-04-30 13:13:52 +00:00
</refsect1>
2006-08-02 15:43:50 +00:00
<refsect1 role= "seealso" >
&reftitle.seealso;
<para >
<simplelist >
2012-03-05 10:45:19 +00:00
<member > <interfacename > JsonSerializable</interfacename> </member>
2006-08-02 15:43:50 +00:00
<member > <function > json_decode</function> </member>
2011-04-30 13:13:52 +00:00
<member > <function > json_last_error</function> </member>
2012-09-02 13:15:00 +00:00
<member > <function > serialize</function> </member>
2006-08-02 15:43:50 +00:00
</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
2009-09-25 07:04:39 +00:00
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
2006-08-02 15:43:50 +00:00
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
-->