沃梦达 / 编程问答 / php问题 / 正文

将多维数组变成一维数组

Turning multidimensional array into one-dimensional array(将多维数组变成一维数组)

本文介绍了将多维数组变成一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I've been banging my head on this one for a while now.

I have this multidimensional array:

Array
(
    [0] => Array
        (
            [0] => foo
            [1] => bar
            [2] => hello
        )

    [1] => Array
        (
            [0] => world
            [1] => love
        )

    [2] => Array
        (
            [0] => stack
            [1] => overflow
            [2] => yep
            [3] => man
        )

And I need to get this:

Array
(
    [0] => foo
    [1] => bar
    [2] => hello
    [3] => world
    [4] => love
    [5] => stack
    [6] => overflow
    [7] => yep
    [8] => man
)

Any ideas?

All other solutions I found solve multidimensional arrays with different keys. My arrays use simple numeric keys only.

解决方案

array_reduce($array, 'array_merge', array())

Example:

$a = array(array(1, 2, 3), array(4, 5, 6));
$result = array_reduce($a, 'array_merge', array());

Result:

array[1, 2, 3, 4, 5, 6];

这篇关于将多维数组变成一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将多维数组变成一维数组

基础教程推荐