Home  >  Article  >  Backend Development  >  How to convert two-dimensional array to one-dimensional array in PHP

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

小云云
小云云Original
2018-03-29 16:26:572002browse

This article mainly shares with you the method of converting PHP two-dimensional array into one-dimensional array. It is mainly shared with you in the form of code. I hope it can help you.

$arr = [
    'a' => [
        1,
        2,
    ],
    'b' => [
        '3',
        '4'=>[
            '5'
        ]
    ],
    'b'
];
print_r(iterator_to_array(
        new RecursiveIteratorIterator(
            new RecursiveArrayIterator($arr)
        ),
        false
    )
);

Result:

Array ([0] => 1 [1] => 2 [2] => 3 [3] => 5 [4 ] => b )

Related recommendations:

php Convert a two-dimensional array into an array with a parent-child relationship_PHP tutorial

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