Home > Backend Development > PHP Problem > How to convert json data into array in php

How to convert json data into array in php

PHPz
Release: 2023-04-14 17:50:00
Original
757 people have browsed it

Preface

In Web development, JSON, as a commonly used data format, is widely used in data interaction and storage. In use, JSON data needs to be converted into an array for processing. This article will introduce how to convert JSON data into an array in PHP.

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read, write and understand. It is based on a subset of JavaScript syntax, but supports a variety of programming languages. Data in JSON format is represented by key-value pairs. Keys and values ​​are separated by colons (:), key-value pairs are separated by commas (,), and the whole is represented by curly braces ({}) or square brackets ([] )Include.

JSON data example:

{
    "name": "John",
    "age": 30,
    "married": true,
    "hobbies": ["music", "reading", "traveling"],
    "address": {
        "city": "New York",
        "street": "Broadway"
    }
}
Copy after login

Convert JSON data into an array

In PHP, you can use the built-in function json_decode() to convert JSON data into an array. This function accepts two parameters. The first parameter is the JSON string to be parsed, and the second parameter is a Boolean value that specifies whether to convert the JSON object into an associative array. If this parameter is true, the JSON object is converted into an associative array; otherwise, the JSON object is converted into an index array.

Sample code:

$json_str = '{ "name": "John", "age": 30, "married": true, "hobbies": ["music", "reading", "traveling"], "address": { "city": "New York", "street": "Broadway" } }';
$arr = json_decode($json_str, true); // 将JSON数据转为关联数组
print_r($arr);
Copy after login

Output result:

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

    [address] => Array
        (
            [city] => New York
            [street] => Broadway
        )

)
Copy after login

In the above code, the json_decode() function is used to convert JSON data into an associative array. Print the array and you can see that the JSON data has been successfully converted into an array.

It should be noted that if the JSON data contains Unicode characters, they need to be escaped before the json_decode() function. You can use the json_encode() function to escape the original data, or use htmlspecialchars() to escape special characters.

Summary

As a commonly used data format, JSON can be widely used in Web development. In PHP, JSON data can be parsed into an array through the built-in function json_decode(), which facilitates subsequent data processing. In practical applications, attention needs to be paid to escaping issues when data contains special characters.

The above is the detailed content of How to convert json data into array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template