Home  >  Article  >  Backend Development  >  PHP converts multi-dimensional array into one-dimensional array

PHP converts multi-dimensional array into one-dimensional array

PHPz
PHPzOriginal
2023-05-11 10:37:36591browse

PHP is a very flexible and versatile programming language. Among them, multidimensional array is one of the most common data structures in programming. However, on some occasions, we need to flatten multi-dimensional arrays into one-dimensional arrays for unified processing. In this article, we will introduce in detail how to convert a multi-dimensional array into a one-dimensional array.

  1. Use the array_walk_recursive() function to flatten multi-dimensional arrays

In PHP, the array_walk_recursive() function can traverse each element in a multi-dimensional array and then convert it into One-dimensional array. The following is a sample code that uses the array_walk_recursive() function to convert a multi-dimensional array into a one-dimensional array:

function array_flatten($array) {
    $flatten = [];
    array_walk_recursive($array, function($value) use(&$flatten) {
        $flatten[] = $value;
    });
    return $flatten;
}

The sample code uses a closure function to traverse a multi-dimensional array. The array_walk_recursive() function traverses a multi-dimensional array, adds each element to a new one-dimensional array, and finally returns the new array.

  1. Use the array_reduce() function to flatten multi-dimensional arrays

In addition to the array_walk_recursive() function, PHP's array_reduce() function can also flatten multi-dimensional arrays. The following is a sample code that uses the array_reduce() function to convert a multi-dimensional array into a one-dimensional array:

function array_flatten($array) {
    return array_reduce($array, function($result, $item) {
        if (is_array($item)) {
            $result = array_merge($result, array_flatten($item));
        } else {
            $result[] = $item;
        }
        return $result;
    }, []);
}

The sample code uses a recursive function to traverse a multi-dimensional array. By judging whether the element is an array, each element in the array is traversed and merged, and finally the multi-dimensional array is converted into a one-dimensional array.

  1. Use foreach loop to flatten multi-dimensional arrays

If you are not familiar with recursion and functional programming, you can use foreach loop to flatten multi-dimensional arrays. The following is a sample code that uses a foreach loop to convert a multi-dimensional array into a one-dimensional array:

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

The sample code uses a foreach loop to traverse the multi-dimensional array, and then performs further traversal and conversion through recursion. When traversing to an element that is an array, continue traversing and processing the element; when traversing to an element that is not an array, add the element to a new one-dimensional array.

Summary

There are many ways to convert a multi-dimensional array into a one-dimensional array in PHP, including using the array_walk_recursive() function, array_reduce() function, and using recursion and foreach loops wait. Which method to choose should be determined according to the actual situation and used flexibly to achieve the best effect and code clarity.

The above is the detailed content of PHP converts multi-dimensional array into one-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:php delete array lastNext article:php delete array last