将循环内形成的多个数组转换为单个数组

时间:2014-06-15 08:22:44

标签: php arrays

当我循环我需要将此数组格式转换为以下格式的内容时,我正在获取以下类型的数组,下面是我在循环时得到的实际数组。

Array
(
    [jasmine] => Array
        (
            [0] => a
            [1] => bb
            [2] => c
            [3] => d
        )

)
Array
(
    [rose] => Array
        (
            [0] => a
            [1] => 4
            [2] => 0.5
            [3] => d
        )

)
Array
(
    [strawberry] => Array
        (
            [0] => a
            [1] => 42
            [2] => 0.5
            [3] => f
        )
)

我需要的结果数组是下面的一个我怎么能实现这个任何帮助赞赏。提前谢谢。

Array(
    [jasmine] => Array
        (
            [0] => a
            [1] => bb
            [2] => c
            [3] => d
        )
    [rose] => Array
        (
            [0] => a
            [1] => 4
            [2] => 0.5
            [3] => d
        )
    [strawberry] => Array
        (
            [0] => a
            [1] => 42
            [2] => 0.5
            [3] => f
        )
);

1 个答案:

答案 0 :(得分:1)

首先,我将所有数组放在$ arrays var中,以供阅读:

$arrays = array(
    array(
        'jasmine' => array('a','bb','c','d')
    ),
    array(
        'rose' => array('a','4','0.5','d')
    ),
    array(
        'strawberry' => array('a','42','0.5','f')
    )
);

然后数组合并将执行:

$result = array_merge($arrays[0],$arrays[1],$arrays[2]);

结果将是(var_dump):

array(3) { ["jasmine"]=> array(4) { [0]=> string(1) "a" [1]=> string(2) "bb" [2]=> string(1) "c" [3]=> string(1) "d" } ["rose"]=> array(4) { [0]=> string(1) "a" [1]=> string(1) "4" [2]=> string(3) "0.5" [3]=> string(1) "d" } ["strawberry"]=> array(4) { [0]=> string(1) "a" [1]=> string(2) "42" [2]=> string(3) "0.5" [3]=> string(1) "f" } }