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

How to convert json to array in php

Apr 20, 2023 am 10:07 AM

In PHP, converting JSON to an array is a common operation. JSON is a lightweight data exchange format that is increasingly popular among developers because of its readability and portability. When using PHP for JSON processing, you can use PHP's built-in functions to convert JSON data into an array. In this article, we will learn how to convert JSON to array using PHP.

  1. Use the json_decode() function to convert JSON into an array

PHP provides a function named "json_decode()", which can convert JSON data into PHP array. This function takes two parameters, the first is the JSON string you want to convert, and the second is an optional boolean parameter that specifies whether to convert the JSON to an associative array. By default, the value of this parameter is false, which means a normal array is returned.

The following is an example of a JSON string:

1

2

3

4

5

$json_string = '{

  "name""John",

  "age": 30,

  "city""New York"

}';

Copy after login

Convert the JSON string into an associative array by calling the json_decode() function:

1

$array = json_decode($json_string, true);

Copy after login

Now, we The value of JSON data can be obtained by accessing the key of the array, for example:

1

2

3

echo $array['name']; // 输出:John

echo $array['age']; // 输出:30

echo $array['city']; // 输出:New York

Copy after login
  1. Supports JSON Chinese parsing

If the JSON string contains Chinese characters, it is required Special processing is required for correct parsing. You can use the PHP built-in function json_decode() in combination with json_last_error_msg() to solve this problem.

1

2

3

4

5

6

7

8

9

$json_string '{"中文": "这是一个中文字符串"}';

$array = json_decode($json_string, true);

 

if (json_last_error() === JSON_ERROR_UTF8) {

    $json_string = mb_convert_encoding($json_string'UTF-8''UTF-8');

    $array = json_decode($json_string, true);

}

 

echo $array['中文']; // 输出:这是一个中文字符串

Copy after login
  1. When there are multiple levels of nesting in the JSON string

When there are multiple levels of nesting in the JSON data, you can use the recursive method to convert the JSON Convert to multidimensional array. As shown below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

$json_string = '{

  "name""John",

  "age": 30,

  "city""New York",

  "children": [

    {

      "name""Mary",

      "age": 5

    },

    {

      "name""Bob",

      "age": 3

    }

  ]

}';

$array = json_decode($json_string, true);

 

function json_to_array($data) {

    if (is_object($data)) {

        $data = get_object_vars($data);

    }

    if (is_array($data)) {

        return array_map(__FUNCTION__$data);

    else {

        return $data;

    }

}

 

$array = json_to_array($array);

 

print_r($array); // 输出:

/*

Array

(

    [name] => John

    [age] => 30

    [city] => New York

    [children] => Array

        (

            [0] => Array

                (

                    [name] => Mary

                    [age] => 5

                )

 

            [1] => Array

                (

                    [name] => Bob

                    [age] => 3

                )

 

        )

 

)

*/

Copy after login

In this article we introduce several methods to convert JSON to an array. Whether it is from a simple JSON string or multi-layer nested JSON data, PHP's json_decode() function can complete the parsing task very well. I hope this article can help you better use PHP to process JSON data.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24