How to convert 2D array to 1D array in PHP

WBOY
Release: 2023-07-07 18:46:01
Original
2548 people have browsed it

How to convert a two-dimensional array to a one-dimensional array in PHP

In PHP development, you often encounter scenarios where you need to convert a two-dimensional array into a one-dimensional array. This article will introduce several common methods to help you complete this task easily.

Method 1: Use loop traversal

The simplest and most straightforward method is to use a loop to traverse the two-dimensional array and add each element to a new one-dimensional array. The following is a code example using this method:

function flattenArray($array) {
    $result = [];
    foreach ($array as $subArray) {
        foreach ($subArray as $element) {
            $result[] = $element;
        }
    }
    return $result;
}

// 测试
$twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
$oneDimensionalArray = flattenArray($twoDimensionalArray);
print_r($oneDimensionalArray);
Copy after login

Run the above code, the output should be:

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

Method 2: Use the array_reduce function

PHP provides a powerful The array_reduce function can be used to convert a two-dimensional array into a one-dimensional array. The array_reduce function accepts an array to be processed and a callback function as parameters, which is used to decide how to gradually reduce the values ​​in the array to a single value. The following is a code example using the array_reduce function:

function flattenArray($array) {
    return array_reduce($array, function($carry, $item) {
        return array_merge($carry, $item);
    }, []);
}

// 测试
$twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
$oneDimensionalArray = flattenArray($twoDimensionalArray);
print_r($oneDimensionalArray);
Copy after login

Run the above code, the output should be the same one-dimensional array.

Method 3: Use the array_merge function

The array_merge function can also be used to convert a two-dimensional array into a one-dimensional array. The array_merge function merges multiple arrays into one array and returns the result. The following is a code example using the array_merge function:

function flattenArray($array) {
    return call_user_func_array('array_merge', $array);
}

// 测试
$twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
$oneDimensionalArray = flattenArray($twoDimensionalArray);
print_r($oneDimensionalArray);
Copy after login

Run the above code, the output should be the same one-dimensional array.

Method 4: Use the array_walk_recursive function

The array_walk_recursive function is used to recursively traverse each element in the array and perform callback processing. The following is a code example using the array_walk_recursive function:

function flattenArray(&$array) {
    $result = [];
    array_walk_recursive($array, function($item) use (&$result) {
        $result[] = $item;
    });
    return $result;
}

// 测试
$twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
$oneDimensionalArray = flattenArray($twoDimensionalArray);
print_r($oneDimensionalArray);
Copy after login

Run the above code, the output should be the same one-dimensional array.

Summary

This article introduces four common methods to convert a two-dimensional array into a one-dimensional array in PHP. Choosing a method that suits your project needs and personal habits can improve the readability and efficiency of your code. The first of these methods is the most common and straightforward, but other methods may be more effective in certain scenarios. I hope this article can help you better understand and apply these methods to easily convert two-dimensional arrays to one-dimensional arrays during development.

The above is the detailed content of How to convert 2D array to 1D 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!