Home  >  Article  >  Backend Development  >  How to convert php json to array

How to convert php json to array

PHPz
PHPzOriginal
2023-04-26 09:10:42440browse

In the process of PHP programming, it is often necessary to convert JSON format data into PHP arrays. This is useful both for handling front-end output and back-end data storage. This article will introduce how to convert JSON format data into a PHP array through PHP's built-in functions.

Step One: Understand JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format. Its format is similar to objects and arrays in JavaScript, so it can be used by many programming languages. The standard JSON format is as follows:

{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

The JSON array format is as follows:

[
    "apple",
    "banana",
    "orange"
]

Step 2: Use the json_decode() function

PHP built-in functionjson_decode() Can convert JSON string to PHP array.

$json_data = '{"name": "John Doe", "age": 30, "city": "New York"}';
$array_data = json_decode($json_data, true);

print_r($array_data);

Output result:

Array
(
    [name] => John Doe
    [age] => 30
    [city] => New York
)

Step 3: Process the array in the JSON object

If the JSON string contains an array, you need to pay attention to some issues during the conversion process.

For example, the following JSON data contains an array:

{
   "name": "John Doe",
   "age": 30,
   "hobbies": ["reading", "swimming", "traveling"]
}

Use the json_decode() function to convert it to a PHP array:

$json_data = '{"name": "John Doe", "age": 30, "hobbies": ["reading", "swimming", "traveling"]}';
$array_data = json_decode($json_data, true);

print_r($array_data);

Output result:

Array
(
    [name] => John Doe
    [age] => 30
    [hobbies] => Array
        (
            [0] => reading
            [1] => swimming
            [2] => traveling
        )
)

You can see that the value of hobbies is converted into a PHP array. If you need to access the elements in this array, just use the array subscript. For example:

echo $array_data['hobbies'][0]; //输出:reading

Step 4: Process objects in the JSON array

Similarly, if the JSON string contains objects, you need to pay attention to some issues during the conversion process.

For example, the following JSON array contains objects:

[
    {
        "name": "John Doe",
        "age": 30,
        "city": "New York"
    },
    {
        "name": "Jane Smith",
        "age": 25,
        "city": "Los Angeles"
    }
]

Use the json_decode() function to convert it to a PHP array:

$json_data = '[{"name": "John Doe", "age": 30, "city": "New York"}, {"name": "Jane Smith", "age": 25, "city": "Los Angeles"}]';
$array_data = json_decode($json_data, true);

print_r($array_data);

Output results:

Array
(
    [0] => Array
        (
            [name] => John Doe
            [age] => 30
            [city] => New York
        )

    [1] => Array
        (
            [name] => Jane Smith
            [age] => 25
            [city] => Los Angeles
        )
)

As you can see, the entire JSON array is converted into a PHP array. Each element of the array is an associative array, corresponding to the attributes of each object in JSON. Properties in an object can be accessed using array subscripts. For example:

echo $array_data[0]['name']; //输出:John Doe

Summary

The above is the relevant knowledge and methods for converting JSON format data into PHP arrays. It is worth noting that when converting JSON format data, the conversion may fail due to incorrect JSON format or array structure problems. Therefore, when using the json_decode() function for conversion, you need to ensure that the JSON format is correct and that the corresponding PHP array structure matches the JSON data.

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