Home Backend Development PHP Problem php 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"
    ]
}
Copy after login
Copy after login

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]]])
Copy after login

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);
Copy after login

Output result:

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

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

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]])
Copy after login

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;
Copy after login

Output result:

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

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)