Home  >  Article  >  Backend Development  >  How to convert php json data into an array

How to convert php json data into an array

PHPz
PHPzOriginal
2023-04-26 14:19:11506browse

In Web development, JSON (JavaScript Object Notation) has become a commonly used data format. It can serialize complex data structures into strings and then transmit them over the network. In PHP, we often need to convert JSON data into arrays to facilitate our operations on them.

This article will introduce how to convert JSON data into an array in PHP. We will use PHP's own json_decode() function to accomplish this task. The json_decode() function can convert a JSON-formatted string into a PHP object or array. If the passed JSON data cannot be decoded, the json_decode() function will return NULL.

First, we need to prepare a piece of data in JSON format. Here, we will use the following JSON data:

{
    "name": "John Smith",
    "age": 28,
    "email": "john.smith@example.com",
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": 12345
    },
    "phone": [
        {
            "type": "home",
            "number": "555-1234"
        },
        {
            "type": "work",
            "number": "555-5678"
        }
    ]
}

Next, we can use the file_get_contents() function to read this JSON formatted string into a variable:

$json_data = file_get_contents('data.json');

Next , we can use the json_decode() function to convert this JSON-formatted string into an array:

$data = json_decode($json_data, true);

Here, we set the second parameter to true, indicating that we want to convert the JSON data into an associative array . If the second parameter is not true, the function will return an object.

Now, we can use the print_r() function to view the converted array:

print_r($data);

The output is as follows:

Array
(
    [name] => John Smith
    [age] => 28
    [email] => john.smith@example.com
    [address] => Array
        (
            [street] => 123 Main St
            [city] => Anytown
            [state] => CA
            [zip] => 12345
        )

    [phone] => Array
        (
            [0] => Array
                (
                    [type] => home
                    [number] => 555-1234
                )

            [1] => Array
                (
                    [type] => work
                    [number] => 555-5678
                )

        )

)

We can see that we successfully Convert JSON data to an array. Now we can operate on it like any array.

In this article, we introduced how to convert JSON data into an array in PHP. We use the json_decode() function that comes with PHP to complete this task. The json_decode() function can convert a JSON-formatted string into a PHP object or array. If the passed JSON data cannot be decoded, the json_decode() function will return NULL.

The above is the detailed content of How to convert php json data into an array. 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