How to convert json into array object array object in php

王林
Release: 2023-05-06 12:10:08
Original
422 people have browsed it

During the encoding process, transmitting data in JSON format is a very common operation. PHP provides a function called json_decode() for converting JSON strings into PHP values. This article explains how to use PHP to convert JSON to arrays and objects.

Convert to array using json_decode()

Suppose we have the following JSON string:

{
    "name": "Tom",
    "age": 30,
    "hobbies": ["reading", "running", "swimming"],
    "address": {
        "city": "Beijing",
        "country": "China"
    }
}
Copy after login

Now we need to convert it to a PHP array. We can use the json_decode() function and set the second parameter to true to convert the JSON string into an associative array. The code is as follows:

$jsonString = '{"name":"Tom","age":30,"hobbies":["reading","running","swimming"],"address":{"city":"Beijing","country":"China"}}';
$assocArray = json_decode($jsonString, true);
print_r($assocArray);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 30
    [hobbies] => Array
        (
            [0] => reading
            [1] => running
            [2] => swimming
        )

    [address] => Array
        (
            [city] => Beijing
            [country] => China
        )

)
Copy after login

As you can see, we successfully converted the JSON string into a PHP array and printed the result.

Use json_decode() Convert to object

In addition to converting a JSON string to a PHP array, we can also convert it to a PHP object. Similarly, we can use the json_decode() function and set the second parameter to false or omit it to convert the JSON string into an object, the code is as follows:

$jsonString = '{"name":"Tom","age":30,"hobbies":["reading","running","swimming"],"address":{"city":"Beijing","country":"China"}}';
$obj = json_decode($jsonString);
var_dump($obj);
Copy after login

The output result is:

object(stdClass)#1 (4) {
  ["name"]=>
  string(3) "Tom"
  ["age"]=>
  int(30)
  ["hobbies"]=>
  array(3) {
    [0]=>
    string(7) "reading"
    [1]=>
    string(7) "running"
    [2]=>
    string(8) "swimming"
  }
  ["address"]=>
  object(stdClass)#2 (2) {
    ["city"]=>
    string(7) "Beijing"
    ["country"]=>
    string(5) "China"
  }
}
Copy after login

As you can see, we successfully converted the JSON string into a PHP object and printed the result.

Use json_decode() Convert to object array

In some cases, we need to convert a JSON string into multiple PHP objects. In this case, we can first convert it to a PHP array and then use the array mapping function to convert it to an array of PHP objects. The code is as follows:

$jsonString = '[{"name":"Tom","age":30},{"name":"Alice","age":25},{"name":"Bob","age":40}]';
$array = json_decode($jsonString, true);
$objArray = array_map(function($item) {
    return (object) $item;
}, $array);
print_r($objArray);
Copy after login

The output result is:

Array
(
    [0] => stdClass Object
        (
            [name] => Tom
            [age] => 30
        )

    [1] => stdClass Object
        (
            [name] => Alice
            [age] => 25
        )

    [2] => stdClass Object
        (
            [name] => Bob
            [age] => 40
        )

)
Copy after login

As you can see, we successfully converted the JSON string into a PHP object array and printed the result.

In short, PHP provides a very convenient way to convert JSON strings into PHP arrays and objects. We just need to use the json_decode() function and specify the appropriate parameters. This is useful because most web APIs return data in JSON.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!