Home >Backend Development >PHP Problem >How to convert php json into object array object

How to convert php json into object array object

PHPz
PHPzOriginal
2023-03-29 11:34:39735browse

Converting json into objects, arrays, and object arrays are very common operations in PHP. In web development, it is very common to use json data format in front-end and back-end communication, and the json extension function provided in PHP makes it very easy to process json format data.

This article will introduce how to convert json data into objects, arrays, and object arrays.

  1. Convert json data into objects

Use the json_decode() function to convert json data into objects, as shown below:

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

The above code will The string in json format is converted into object $obj. Note that the json_decode() function converts json data into PHP's stdClass object by default.

You can also convert json data into an associative array in PHP, as shown below:

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

The above code converts json data into an associative array $arr. By passing the second parameter as true in the json_decode() function, you can convert the json data into a PHP associative array.

  1. Convert json data into an array

To convert json data into an array, just set the second parameter to true in the json_decode() function, As shown below:

$json_string = '[{"name":"Tom","age":20},{"name":"Jane","age":19}]';
$arr = json_decode($json_string,true);

The above code converts json array data into PHP's associative array $arr.

  1. Convert json data into an object array

Converting json data into an object array also requires setting the second parameter to true in the json_decode() function, but the conversion What is obtained is an object array, as shown below:

$json_string = '[{"name":"Tom","age":20},{"name":"Jane","age":19}]';
$arr_obj = json_decode($json_string);

The above code converts json array data into object array $arr_obj. Each json data item can be converted into an object without passing the second parameter in the json_decode() function.

You can use a foreach loop to traverse the object array, as shown below:

foreach ($arr_obj as $item) {
    echo "name: " . $item->name . "; age: " . $item->age . "\n";
}

The above code can output the name and age attributes of each object in the object array.

The above is the method to convert json data into objects, arrays, and object arrays. I hope readers can flexibly use json extension functions in actual development to quickly process json data.

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