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

How to convert json string into array object in php

PHPz
PHPzOriginal
2023-04-04 09:17:46476browse

As a very popular programming language, PHP has many practical functions that can help us solve various problems. When we need to convert a Json string into an array object, we can use the PHP built-in function json_decode().

json_decode()The function is used to decode the Json string and convert it into a PHP variable. It should be noted that the converted variable type is based on the data type in the Json string. The determined value can be a string, number, array, object, Boolean value or null.

The following is an example code to convert a Json string into an array object:

$data = '{ "name":"John", "age":30, "city":"New York" }';
$json = json_decode($data);
print_r($json);

The running result is:

stdClass Object ( [name] => John [age] => 30 [city] => New York )

A Json string is used here{ "name":"John", "age":30, "city":"New York" }, and then convert it into a stdClass object through the json_decode() function. stdClass is a class in PHP, representing an empty object that does not contain any properties, methods or variables.

This method is very suitable for converting simple Json strings, but when we encounter complex Json strings, more processing methods are needed to complete the conversion of complex structures. For example, when the Json string contains an array:

$data = '["Google", "Facebook", "Apple", "Microsoft"]';
$json = json_decode($data);
print_r($json);

The running result is:

Array ( [0] => Google [1] => Facebook [2] => Apple [3] => Microsoft )

We can see that an array containing multiple string elements is used here[" Google", "Facebook", "Apple", "Microsoft"], and then convert it into a PHP array through the json_decode() function.

Similarly, when we need to convert the object contained in the Json string into a PHP variable, we can also use the json_decode() function. For example:

$data = '{ "name":"John", "age":30, "city":"New York" }';
$json = json_decode($data, true);
print_r($json);

The running result is:

Array ( [name] => John [age] => 30 [city] => New York )

The json_decode() function is used here, and the second parameter true is used, which means Returns an array instead of a stdClass object.

As you can see from the above examples, converting Json strings into array objects is a very practical function, and PHP's built-in json_decode() function can provide us with quick and convenient s solution. It should be noted that some special characters in the Json string, such as backslash \, etc., require special processing to be parsed correctly. Special attention is required when using it.

The above is the detailed content of How to convert json string into array 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