svn: /phpdoc/en/trunk/reference/json/functions/ json-encode.xml

From: Date: Tue, 06 Dec 2011 23:36:32 +0000
Subject: svn: /phpdoc/en/trunk/reference/json/functions/ json-encode.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-9170@lists.php.net to get a copy of this message
frozenfire Tue, 06 Dec 2011 23:36:32 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=320527 Log: Noted non-sequential array encoding behaviour, and added example. Closes bug #60418. Bug: https://bugs.php.net/60418 (Assigned) json_encode() => Object and Array conversion behavior Changed paths: U phpdoc/en/trunk/reference/json/functions/json-encode.xml Modified: phpdoc/en/trunk/reference/json/functions/json-encode.xml =================================================================== --- phpdoc/en/trunk/reference/json/functions/json-encode.xml 2011-12-06 22:10:15 UTC (rev 320526) +++ phpdoc/en/trunk/reference/json/functions/json-encode.xml 2011-12-06 23:36:32 UTC (rev 320527) @@ -174,6 +174,76 @@ ]]> </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"}" +]]> + </screen> + </example> </para> </refsect1> @@ -185,6 +255,13 @@ can be used to determine the exact nature of the error. </para> </note> + <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> </refsect1> <refsect1 role="seealso">

« previous php.doc.cvs (#9170) next »