In PHP, you can use the json_encode() function to convert associative arrays into json data. This function can JSON encode PHP variables (arrays, objects, etc.) and convert them into json format data; Syntax "json_encode($array)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the json_encode() function To convert associative array to json data.
<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); ?>
Output result:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Description:
json_encode - JSON encoding of variables.
json_encode($value, $options, $depth)
Parameters:
value: The value to be encoded can be any data type except the resource type.
options: Binary mask consisting of the following constants
JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE , JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR. For details about JSON constants, please refer to the JSON Constants page.
depth: Set the maximum depth. Must be greater than 0.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert associative array to json data in php. For more information, please follow other related articles on the PHP Chinese website!