Home > Backend Development > PHP Problem > How to convert JSON to one dimensional array in php

How to convert JSON to one dimensional array in php

PHPz
Release: 2023-04-25 10:18:34
Original
702 people have browsed it

JSON is a data exchange format mainly used to transfer data between clients and servers. In PHP, JSON data can be decoded into a PHP array or object through the json_decode() function. Sometimes, we need to convert JSON data into a one-dimensional array. This process is relatively simple. This article will explain in detail how to convert JSON into a one-dimensional array.

Idea Analysis

The main idea of ​​converting JSON data into a one-dimensional array is:

  1. Use the json_decode() function to decode the JSON data into a PHP array.
  2. Use a recursive function to traverse each element in the PHP array;
  3. Save each element into a one-dimensional array.

Code implementation

The following is a sample code that demonstrates converting JSON to a one-dimensional array:

function json_to_array($json) {
    $result = array();
    $jsonArray = json_decode($json, true);
    if (is_array($jsonArray)) {
        foreach ($jsonArray as $key => $json) {
            if (is_array($json)) {
                $result = array_merge($result, json_to_array(json_encode($json)));
            } else {
                $result[] = $json;
            }
        }
    }
    return $result;
}

$json = '{"a":1,"b":[1,2,3],"c":{"d":4,"e":5}}';
$arr = json_to_array($json);
print_r($arr);
Copy after login

In this sample code, we first define a The recursive function json_to_array() is used to convert JSON into a one-dimensional array. This function accepts a JSON string and returns a one-dimensional array.

First use the json_decode() function to decode the JSON string into a PHP array. If the decoding fails, an empty array will be returned directly.

We then use foreach() to loop through each element in the array, and if the element is an array, recursively call the json_to_array() function and merge the returned one-dimensional array into the resulting array ; Otherwise, add the element to the result array.

Finally, we use a test example to demonstrate how to use the above function. This example first defines a JSON string containing nested arrays, then uses the json_to_array() function to convert it into a one-dimensional array, and prints the result. .

The test results are as follows:

Array
(
    [0] => 1
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)
Copy after login

Conclusion

Through the above example code, we can see that converting JSON into a one-dimensional array is relatively simple. In actual development, we can use this method to handle some requirements that require separate key data in JSON, and can also be used to handle data exchange and conversion between JSON and PHP.

However, it should be noted that this method has performance and efficiency problems, which may have some impact on PHP performance. Therefore, in practical applications, the most appropriate data processing solution must be selected based on the actual situation.

The above is the detailed content of How to convert JSON to one dimensional 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