Home > Common Problem > How to convert a two-dimensional php array into a one-dimensional array

How to convert a two-dimensional php array into a one-dimensional array

百草
Release: 2023-08-03 11:14:30
Original
4533 people have browsed it

How to convert a two-dimensional array into a one-dimensional array in php: 1. Use loop traversal to traverse the two-dimensional array and add each element to the one-dimensional array; 2. Use the "array_merge" function, you can Merge multiple arrays into one array, pass the two-dimensional array as a parameter to the "array_merge" function, and convert it into a one-dimensional array; 3. Use the "array_reduce" function to pass all the values ​​in the array through a callback function Perform processing and finally return a result.

How to convert a two-dimensional php array into a one-dimensional array

The operating system of this tutorial: Windows10 system, PHP version 8.1.3, DELL G3 computer.

In PHP programming, sometimes we need to convert a two-dimensional array into a one-dimensional array. A two-dimensional array is composed of multiple one-dimensional arrays, and each one-dimensional array may also contain other arrays. Here are a few common ways to convert a two-dimensional array into a one-dimensional array.

Method 1: Use loop traversal

The most direct way is to use a loop to traverse the two-dimensional array and add each element to the one-dimensional array. The following is a simple sample code:

$twoDimensionalArray = array(
    array("apple", "banana", "orange"),
    array("car", "bike", "motorcycle"),
    array("sun", "moon", "stars")
);
$oneDimensionalArray = array();
foreach ($twoDimensionalArray as $array) {
    foreach ($array as $element) {
        $oneDimensionalArray[] = $element;
    }
}
print_r($oneDimensionalArray);
Copy after login

In the above code, we use two nested loops to first traverse each one-dimensional array in the two-dimensional array, and then traverse each one-dimensional array. elements and add them to a one-dimensional array. Finally, the results are output through the `print_r` function.

Method 2: Use the `array_merge` function

PHP provides the `array_merge` function, which can merge multiple arrays into one array. We can pass a two-dimensional array as a parameter to the `array_merge` function to convert it into a one-dimensional array. Below is a sample code using the `array_merge` function:

$twoDimensionalArray = array(
    array("apple", "banana", "orange"),
    array("car", "bike", "motorcycle"),
    array("sun", "moon", "stars")
);
$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
print_r($oneDimensionalArray);
Copy after login

In the above code, we use the `call_user_func_array` function to pass a two-dimensional array as a parameter to the `array_merge` function. The first parameter of the `call_user_func_array` function is the name of the function to be called, and the second parameter is the array passed as a parameter. Finally, the results are output through the `print_r` function.

Method 3: Use the `array_reduce` function

Another method is to use the `array_reduce` function, which can process all the values ​​in the array through a callback function and finally return a result. The following is a sample code using the `array_reduce` function:

$twoDimensionalArray = array(
    array("apple", "banana", "orange"),
    array("car", "bike", "motorcycle"),
    array("sun", "moon", "stars")
);
$oneDimensionalArray = array_reduce($twoDimensionalArray, function ($carry, $item) {
    return array_merge($carry, $item);
}, []);
print_r($oneDimensionalArray);
Copy after login

In the above code, we use the `array_reduce` function to traverse the two-dimensional array and pass a callback function. The callback function receives two parameters, `$carry` represents the return value of the last callback function (the initial value is an empty array), and `$item` represents the currently traversed one-dimensional array. In the callback function, we use the `array_merge` function to merge the elements in each one-dimensional array into `$carry`. Finally, the results are output through the `print_r` function.

No matter which method you choose, you can convert a two-dimensional array into a one-dimensional array. According to actual needs, you can choose the method that best suits you to handle the two-dimensional array conversion problem.

The above is the detailed content of How to convert a two-dimensional php array into a one-dimensional array. 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