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

How to convert json string to array in php

PHPz
Release: 2023-04-20 11:08:41
Original
1698 people have browsed it

In PHP, we often need to convert JSON strings into array format to operate data, so how to implement this function? Next, this article will take you step by step to learn how to convert a JSON string into an array.

1. What is JSON?

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and easy to write. Its syntax is similar to XML, but JSON is more concise and clear, and is suitable for data transmission and storage.

JSON specifies two data structures, namely "key-value pair" and "array". Among them, "key-value pair" refers to a mapping between a set of keys and values, and "array" is an ordered list of values.

A simple JSON data example:

{
  "name": "小明",
  "age": 18,
  "hobbies": ["听音乐", "读书", "旅游"]
}
Copy after login

2. The json_decode function in PHP

In PHP, we can use the json_decode function to convert the JSON string into array format .

The usage is as follows:

$array = json_decode($jsonString, true);
Copy after login

Among them, $jsonString represents the JSON string to be converted, and true represents the conversion result to be converted into an associative array.

The following is a sample code:

$json = '{"name": "小明", "age": 18, "hobbies": ["听音乐", "读书", "旅游"]}';
$array = json_decode($json, true);
print_r($array);
Copy after login

Output result:

Array
(
    [name] => 小明
    [age] => 18
    [hobbies] => Array
        (
            [0] => 听音乐
            [1] => 读书
            [2] => 旅游
        )

)
Copy after login

In the above code, we passed in a "key-value pair" and "array" JSON string, then use the json_decode function to convert it into an array, and print the result.

3. Notes on the json_decode function in PHP

  1. The key names in the JSON string must be enclosed in double quotes, not single quotes.
  2. Array elements in JSON strings must be enclosed in square brackets [].
  3. Strings within array elements must be enclosed in double quotes, not single quotes.
  4. The json_decode function can only convert UTF-8 encoded JSON strings into PHP arrays.
  5. If decoding fails, null will be returned.

4. Summary

This article introduces in detail the method of converting JSON strings into arrays in PHP, and introduces the precautions of the json_decode function. After studying this article, I believe that readers can master the basic method of converting JSON strings into arrays. I hope it will be helpful to you.

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