Home Backend Development PHP Problem How to convert a string to a JSON array in PHP

How to convert a string to a JSON array in PHP

Apr 26, 2023 am 10:21 AM

In PHP, we often need to convert a string into a JSON array. The process is not difficult, you just need to use some functions provided by PHP.

First of all, we need to make it clear that JSON code in PHP is a lightweight data exchange format. It is easy to read, easy to parse, and easy to process. Therefore, in PHP, converting strings to JSON arrays allows us to process data more conveniently.

Next, we give a simple example to demonstrate how to convert a string into a JSON array in PHP.

Suppose we have a string with the following content:

{"name":"Tom","age":25,"address":"New York"}
Copy after login

We need to convert it into a PHP array, which contains all the fields in this string. We can use the json_decode() function to implement this process.

Here is a code example:

$str = '{"name":"Tom","age":25,"address":"New York"}';
$array = json_decode($str, true);

print_r($array);
Copy after login

When we run this code, it will convert the string into a PHP array and print the array out. The output is as follows:

Array
(
    [name] => Tom
    [age] => 25
    [address] => New York
)
Copy after login

It can be seen from the output that the code has successfully converted the string into a PHP array. The function of json_decode() is to decode the JSON string into a PHP array.

It should be noted that when using the json_decode() function, we need to pass a parameter. This parameter specifies what type of PHP object we want to convert the JSON string into. In the above example, we passed the second parameter true, which means converting the JSON string to an associative array.

Through the above example, we have learned how to convert a string into a PHP array. Next, let's look at another example that shows how to convert a JSON string of multiple records into a PHP array.

Suppose we have the following JSON string:

[
    {"name":"Tom","age":25,"address":"New York"},
    {"name":"John","age":30,"address":"London"},
    {"name":"Bob","age":35,"address":"Paris"}
]
Copy after login

This string contains three records, and we need to convert them into PHP arrays. The following is the code example given:

$str = '[
    {"name":"Tom","age":25,"address":"New York"},
    {"name":"John","age":30,"address":"London"},
    {"name":"Bob","age":35,"address":"Paris"}
]';

$array = json_decode($str, true);

print_r($array);
Copy after login

When we run the above code, the output is as follows:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 25
            [address] => New York
        )

    [1] => Array
        (
            [name] => John
            [age] => 30
            [address] => London
        )

    [2] => Array
        (
            [name] => Bob
            [age] => 35
            [address] => Paris
        )

)
Copy after login

As can be seen, we have successfully converted the JSON string into a PHP array, And the array contains all data records.

To summarize, converting a string to a JSON array in PHP is very simple. We only need to use the json_decode() function and specify the second parameter as true to decode the JSON string into a PHP array.

The above is the detailed content of How to convert a string to a JSON array in PHP. 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)