Home  >  Article  >  Backend Development  >  How to convert string array to json object in php?

How to convert string array to json object in php?

青灯夜游
青灯夜游Original
2020-07-15 18:36:043765browse

In PHP, you can use the json_encode(string array, true) method to convert a string array into a json object. Note: The default return value of json_encode is not a JSON object, but a JSON format string; if you want to convert it into a json object, you need to add the parameter true at the end.

How to convert string array to json object in php?

PHP json_encode() is used to JSON encode variables. This function returns JSON data if executed successfully, otherwise it returns FALSE.

Syntax

string json_encode ( $value [, $options = 0 ] )

Parameters

  • value: The value to encode. This function is only valid for UTF-8 encoded data.

  • options: Binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT

##Example: Convert string array to json object

Define an array and call the json_encode method to encode the array into a string in json format. It should be noted here that the return value of the json_encode method is not A JSON object is a JSON format string. If you want to convert it into a json object, you need to add the parameter true at the end.

After finally converting to json, you need to output the encoded json format string:

How to convert string array to json object in php?

Output:

How to convert string array to json object in php?

Recommended learning:

php tutorial

The above is the detailed content of How to convert string array to json object in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn