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

How to convert json to array in php

PHPz
Release: 2023-04-20 10:59:23
Original
477 people have browsed it

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:

$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:

$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:

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.

$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:

$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!

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