diff --git a/reference/json/constants.xml b/reference/json/constants.xml index 952445d26f..6f83ea4ba8 100644 --- a/reference/json/constants.xml +++ b/reference/json/constants.xml @@ -1,5 +1,5 @@ - + &reftitle.constants; @@ -55,6 +55,71 @@ + + + The following constants can be combined to form options for + json_encode. They are all available as of + PHP 5.3.0. + + + + + JSON_HEX_TAG + (integer) + + + + All < and > are converted to \u003C and \u003E. + + + + + + JSON_HEX_AMP + (integer) + + + + All &s are converted to \u0026. + + + + + + JSON_HEX_APOS + (integer) + + + + All ' are converted to \u0027. + + + + + + JSON_HEX_QUOT + (integer) + + + + All " are converted to \u0022. + + + + + + JSON_FORCE_OBJECT + (integer) + + + + Outputs an object rather than an array when a non-associative array is + used. Especially useful when the recipient of the output is expecting + an object and the array is empty. + + + + + json_encode @@ -39,10 +39,11 @@ options - Bitmask consisting of PHP_JSON_HEX_QUOT, - PHP_JSON_HEX_TAG, - PHP_JSON_HEX_AMP, - PHP_JSON_HEX_APOS. Defaults to 0. + Bitmask consisting of JSON_HEX_QUOT, + JSON_HEX_TAG, + JSON_HEX_AMP, + JSON_HEX_APOS, + JSON_FORCE_OBJECT. Defaults to 0. @@ -72,7 +73,13 @@ 5.2.1 - Added support to JSON encode basic types + Added support to JSON encode basic types. + + + + 5.3.0 + + The options parameter was added. @@ -99,6 +106,51 @@ echo json_encode($arr); + + + + + A <function>json_encode</function> example showing all the options in action + +',"'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"; +?> +]]> + + &example.outputs; + +","'bar'","\"baz\"","&blong&"] +Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&"] +Apos: ["","\u0027bar\u0027","\"baz\"","&blong&"] +Quot: ["","'bar'","\u0022baz\u0022","&blong&"] +Amp: ["","'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}} ]]>