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

How to convert a string to a JSON array in PHP

PHPz
PHPzOriginal
2023-04-26 10:21:31472browse

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

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

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
)

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

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

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
        )

)

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