Convert array to json using php

WBOY
Release: 2023-05-06 10:57:07
Original
510 people have browsed it

In Web development, JSON (JavaScript Object Notation) has become a commonly used data format. It is easy to understand and parse. More and more programmers also choose to use JSON for data transmission and storage.

In PHP development, we sometimes need to convert an array into JSON format, which requires the use of the built-in function json_encode in PHP. However, sometimes we need to convert complex arrays. At this time, we need to splice the arrays, and then convert the spliced ​​results into JSON format. Let's introduce in detail how to convert arrays into JSON in PHP.

1. Splicing arrays

First, we need to splice the arrays to realize the function of converting multi-dimensional arrays into one-dimensional arrays. We can solve this problem recursively. The specific implementation is as follows:

function array_flatten($array) {
    $result = array();
    foreach($array as $value) {
        if(is_array($value)) {
            $result = array_merge($result, array_flatten($value));
        } else {
            $result[] = $value;
        }
    }
    return $result;
}
Copy after login

The above code uses a recursive method to convert the array into a one-dimensional array, where, is_array($value)Determine whether the current value is still an array. If so, continue recursive processing. Otherwise, add the element to the $result array.

2. Convert to JSON format

The spliced ​​array has been converted into a one-dimensional array, and next it needs to be converted into JSON format. We can directly use the json_encode function for conversion. The specific implementation is as follows:

function array_to_json($array) {
    $array = array_flatten($array);
    $json = json_encode($array, JSON_UNESCAPED_UNICODE);
    return $json;
}
Copy after login

In the above code, $array = array_flatten($array)converts a multi-dimensional array into a Dimension array, json_encode($array, JSON_UNESCAPED_UNICODE)Convert the array to JSON format, where JSON_UNESCAPED_UNICODE means that Unicode encoding is not required, thereby retaining Chinese characters.

3. Complete Example

The following is a complete example of converting PHP array to JSON. We define a multi-dimensional array, convert it to JSON format, and output the result.

function array_flatten($array) {
    $result = array();
    foreach($array as $value) {
        if(is_array($value)) {
            $result = array_merge($result, array_flatten($value));
        } else {
            $result[] = $value;
        }
    }
    return $result;
}

function array_to_json($array) {
    $array = array_flatten($array);
    $json = json_encode($array, JSON_UNESCAPED_UNICODE);
    return $json;
}

$array = array(
    array('name' => '张三', 'age' => 20, 'sex' => '男'),
    array('name' => '李四', 'age' => 25, 'sex' => '女'),
    array('name' => '王五', 'age' => 30, 'sex' => '男', 'children' => array('儿子', '女儿')),
);

echo array_to_json($array);
Copy after login

The output result of the above code is:

[
  "张三",
  20,
  "男",
  "李四",
  25,
  "女",
  "王五",
  30,
  "男",
  "儿子",
  "女儿"
]
Copy after login

As you can see, we have successfully converted the multi-dimensional array into a one-dimensional array, and then converted it into JSON format.

4. Summary

This article introduces the method of converting arrays to JSON in PHP, mainly involving two aspects: array splicing and JSON encoding. For some more complex arrays, we can convert them into one-dimensional arrays through splicing, and then easily convert them into JSON format. At the same time, we also explained how to use the json_encode function to convert the array into JSON format.

Of course, in actual development, we can also use third-party libraries to convert arrays into JSON format. For example, the response()->json() function in the Laravel framework can directly convert the array into JSON format and output it, which is simpler and faster.

The above is the detailed content of Convert array to json using 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