Home  >  Article  >  Backend Development  >  How to convert json string to array in php (a brief analysis of the method)

How to convert json string to array in php (a brief analysis of the method)

PHPz
PHPzOriginal
2023-04-12 09:22:52509browse

PHP is a very popular programming language and is widely used in the field of web development. In PHP, JSON is a very common data exchange format, and converting JSON strings to arrays is an essential step when processing JSON data. This article will introduce you how to convert JSON string to array in PHP.

1. What is JSON

The full name of JSON is JavaScript Object Notation, which is JavaScript object notation. It is a lightweight data exchange format. It is based on JavaScript's object representation, but has nothing to do with the JavaScript language and can be parsed and generated by various programming languages.

In JSON, data is generally expressed in the form of "key-value pairs", such as {"key1":"value1","key2":"value2"}. Among them, the key must be of string type, and the value can be various data types such as string, number, Boolean type, array, object, etc. The advantage of this data format is that it is easy to understand and write, read and parse, transmit and process. In web development, JSON is often used for data exchange between front-end and back-end.

2. Method of converting JSON string to array in PHP

PHP provides a very simple method to convert JSON string to array, which is the json_decode() function. This function can decode a JSON formatted string into a PHP array or object.

The syntax of the json_decode() function is as follows:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

Among them, $json is the JSON string to be decoded, $assoc indicates whether to convert the decoded JSON into an associative array, $depth Indicates the depth of conversion, $options indicates the options during conversion.

  1. Convert JSON string to a normal object

When the $assoc parameter is not passed in or is false, json_decode() will convert the decoded JSON into a normal object by default object. For example:

$json_string = '{"name":"Tom","age":18}';
$obj = json_decode($json_string);
var_dump($obj);

Output:

object(stdClass)#1 (2) {
  ["name"]=>
  string(3) "Tom"
  ["age"]=>
  int(18)
}

The above code decodes the JSON string into a normal object. After decoding, you can use the -> operator to get the object's property values.

  1. Convert JSON string to associative array

When the $assoc parameter is set to true, json_decode() converts the decoded JSON into an associative array. For example:

$json_string = '{"name":"Tom","age":18}';
$arr = json_decode($json_string, true);
var_dump($arr);

Output:

array(2) {
  ["name"]=>string(3) "Tom"
  ["age"]=>int(18)
}

The above code decodes the JSON string into an associative array. After decoding, you can use the array index to get the element value of the array.

It should be noted that when converting a JSON string into an associative array, if there are duplicate keys in the JSON, only the last key-value pair will be retained.

  1. Convert JSON string to PHP object

In addition to converting JSON to ordinary objects and associative arrays, the json_decode() function also provides the ability to convert JSON strings For methods of PHP objects. You need to set the $assoc parameter to false and use the stdClass class to create new objects during the decoding process. For example:

$json_string = '{"name":"Tom","age":18}';
$obj = json_decode($json_string, false, 512, JSON_OBJECT_AS_ARRAY);
$php_obj = new stdClass();
foreach($obj as $key => $value) {
    $php_obj->$key = $value;
}
var_dump($php_obj);

Output:

object(stdClass)#4 (2) {
  ["name"]=>
  string(3) "Tom"
  ["age"]=>
  int(18)
}

The above code decodes the JSON string into a PHP object. Use a foreach loop to pass the key-value pairs of the associative array to the new stdClass object, and then use the $-> operator to access the object's property values.

Conclusion

In PHP, converting a JSON string to an array is very simple, just use the json_decode() function. It should be noted that when there are duplicate keys in JSON, only the last key-value pair will be retained.

The above is the detailed content of How to convert json string to array in php (a brief analysis of the method). 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