search
HomeBackend DevelopmentPHP ProblemHow to convert json into array object array object in php

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"
    }
}

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);

The output result is:

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

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

)

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);

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"
  }
}

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);

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
        )

)

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools