Home  >  Article  >  Backend Development  >  How to convert a two-dimensional array into a one-dimensional array in php

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

藏色散人
藏色散人Original
2020-08-11 09:54:292255browse

php How to convert a two-dimensional array into a one-dimensional array: first create a PHP sample file; then define a "reduce" method; then use "array_walk_recursive" and other functional methods to achieve the conversion.

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

Recommended: "PHP Video Tutorial"

php convert two-dimensional array to one-dimensional array

The code is as follows:

/*
@reduce() 多维维数组变成一维数组
@$array 数组
*/
    public function reduce($array)
    {
        $return = [];
        array_walk_recursive($array, function ($x, $index) use (&$return) {
            $return[] = $x;
        });
        return $return;
    }

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