search
HomeBackend DevelopmentPHP Problemphp convert json to array object

php convert json to array object

May 11, 2023 am 09:37 AM

In web development, it often involves submitting data in JSON format from the front-end page to the back-end, and then converting the data into an array or object form that PHP can operate. This article will introduce how to use PHP to convert JSON data into array objects.

JSON is a lightweight data exchange format with clear structure and easy to read and write. PHP has built-in JSON processing functions, which can easily convert JSON into array or object form.

Suppose we have the following JSON data:

{
    "name": "Tom",
    "age": 20,
    "address": {
        "province": "Guangdong",
        "city": "Guangzhou"
    },
    "hobbies": [
        "reading",
        "swimming"
    ]
}

Use PHP's json_decode() function to convert it into an array object. The usage of the function is as follows:

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

Parameter description:

  • $json: JSON string that needs to be decoded.
  • $assoc (optional): When this parameter is true, the array form will be returned, and if it is false, the object form will be returned. Defaults to false.
  • $depth (optional): Set the maximum depth for parsing JSON data. The default is 512, and null will be returned if out of range.
  • $options (optional): Set the JSON decoder options, such as JSON_BIGINT_AS_STRING, which can return large integers as strings.

Code example:

$jsonStr = '{ "name": "Tom", "age": 20, "address": { "province": "Guangdong", "city": "Guangzhou" }, "hobbies": [ "reading", "swimming" ] }';

$array = json_decode($jsonStr, true);

print_r($array);

Output result:

Array
(
    [name] => Tom
    [age] => 20
    [address] => Array
        (
            [province] => Guangdong
            [city] => Guangzhou
        )

    [hobbies] => Array
        (
            [0] => reading
            [1] => swimming
        )
)

As you can see, the json_decode() function converts the JSON string into PHP Array object.

If you want to convert JSON data into PHP object form, you can remove the second parameter in the function or set it to false.

In addition to converting JSON data into array or object form, PHP can also encode data in array or object form into JSON string. This can be achieved using PHP's json_encode() function. Function usage is as follows:

string json_encode(mixed $value[, int $options = 0[, int $depth = 512]])

Parameter description:

  • $value: The value that needs to be encoded as a JSON string.
  • $options (optional): Setting JSON encoder options, such as JSON_PRETTY_PRINT, can make the encoded JSON string easier to read.
  • $depth (optional): Set the maximum depth of encoded JSON data. The default is 512, and an empty string will be returned if it exceeds the range.

Code example:

$array = array(
    'name' => 'Tom',
    'age' => 20,
    'address' => array(
        'province' => 'Guangdong',
        'city' => 'Guangzhou'
    ),
    'hobbies' => array(
        'reading',
        'swimming'
    )
);

$jsonStr = json_encode($array, JSON_PRETTY_PRINT);

echo $jsonStr;

Output result:

{
    "name": "Tom",
    "age": 20,
    "address": {
        "province": "Guangdong",
        "city": "Guangzhou"
    },
    "hobbies": [
        "reading",
        "swimming"
    ]
}

In actual development, AJAX technology is often used to submit JSON data to the back-end server, and then through PHP Convert it into actionable form. Mastering the above JSON-related PHP functions will make it easier to process JSON data.

The above is the detailed content of php convert json to 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

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.