将数组与现有数组php

时间:2017-07-28 15:31:39

标签: php arrays array-merge

我有两个包含数据的数组。我希望能够将它们一起添加,以便第二个数组中的信息连接到第一个数组中。目前我正在做的array_merge将第二个数组添加到第一个数组的末尾。

数组1

[1] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
        )
[2] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
        )

数组2

[1] => Array
        (
            [0] => TIME
            [1] => Lorem
            [2] => Ipsum
        )

[2] => Array
        (
            [0] => TIME
            [1] => Some
            [2] => Text

        )

如何合并两个数组,以便输出如下所示?

数组3

[1] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
            [6] => TIME
            [7] => Lorem
            [8] => Ipsum
        )
[2] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
            [6] => TIME
            [7] => Some
            [8] => Text
        )

目前正在发生什么

[1] => Array
        (
            [0] => 2017-07-14 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
        )

    [2] => Array
        (
            [0] => 2017-07-14 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
        )

    [3] => Array
        (
            [0] => TIME
            [1] => Lorem
            [2] => Ipsum
        )

    [4] => Array
        (
            [0] => TIME
            [1] => Some
            [2] => Text
        )

我尝试了array_merge( $array1 , $array2 );,但是将第二个数组添加到第一个数组的末尾。

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

这看起来很顺利,你只需要array_merge()这两个数组的元素:

<?php
$A = [
    1 => [
        '2017-07-13 00:00:00',
        'Foo',
        'Bar',
        16.11,
        80.56,
        96.67
    ],
    2 => [
        '2017-07-13 00:00:00',
        'Foo',
        'Bar',
        1.23,
        50.69,
        14.24
    ]
];
$B = [
    1 => [
        'TIME',
        'Lorem',
        'Ipsum'
    ],
    2 => [
        'TIME',
        'Some',
        'Text'
    ]
];

array_walk($B, function($values, $key) use (&$A) {
    $A[$key] = array_merge($A[$key], $values);
});
print_r($A);

显然的输出是:

Array
(
    [1] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
            [6] => TIME
            [7] => Lorem
            [8] => Ipsum
        )

    [2] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
            [6] => TIME
            [7] => Some
            [8] => Text
        )
)

更新:

在下面的评论中,您询问是否可以推广此方法以合并任意数量的数组。当然可以,你只需添加另一个迭代层:

<?php
$target = [
    1 => ['2017-07-13 00:00:00', 'Foo', 'Bar', 16.11, 80.56, 96.67],
    2 => ['2017-07-13 00:00:00', 'Foo', 'Bar', 1.23, 50.69, 14.24]
];
$sources = [
    'B' => [
        1 => ['TIME', 'Lorem', 'Ipsum'],
        2 => ['TIME', 'Some', 'Text']
    ],
    'C' => [
        1 => ['C1a', 'C1b'],
        2 => ['C2a', 'C2b', 'C2b']
    ]
];

array_walk($sources, function($source) use (&$target) {
    array_walk($source, function($values, $key) use (&$target) {
        $target[$key] = array_merge($target[$key], $values);
    });
});
print_r($target);

此变体产生输出:

Array
(
    [1] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
            [6] => TIME
            [7] => Lorem
            [8] => Ipsum
            [9] => C1a
            [10] => C1b
        )

    [2] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
            [6] => TIME
            [7] => Some
            [8] => Text
            [9] => C2a
            [10] => C2b
            [11] => C2b
        )

)

答案 1 :(得分:0)

array_merge_recursive怎么样?

  

array_merge_recursive()合并一个或多个数组的元素   一起使得一个值附加到结尾   前一个。它返回结果数组。

$result = array_merge_recursive($first_array, $second_array);

编辑:如果array_merge_recursive不适合您,请尝试https://eval.in/838950

<?php
$first_array = [
    1 => [
        '2017-07-13 00:00:00',
        'Foo',
        'Bar',
        16.11,
        80.56,
        96.67
    ],
    2 => [
        '2017-07-13 00:00:00',
        'Foo',
        'Bar',
        1.23,
        50.69,
        14.24
    ]
];
$second_array = [
    1 => [
        'TIME',
        'Lorem',
        'Ipsum'
    ],
    2 => [
        'TIME',
        'Some',
        'Text'
    ]
];

foreach($second_array as $k=>$v){
 $first_array[$k] = array_merge($frist_array[$k],$v);
}


 print '<pre>';
 /*print_r(array_merge_recursive($first_array,$second_array));*/      
 print_r($frist_array);

答案 2 :(得分:0)

您想要创建多维数组吗?

这很简单。

$new_array[] = $array_1;
$new_array[] = $array_2;

答案 3 :(得分:0)

你可以尝试这种方法:

    $array1 = [
        [1,2,3,4],
        [6,7,8,9]
    ];
    $array2 = [
        ['a','b'],
        ['c', 'd']
    ];
    $array3 = [];
    foreach (range(0, max(count($array1), count($array2))-1) as $i) {
        $part1 = isset($array1[$i]) ? $array1[$i] : [];
        $part2 = isset($array2[$i]) ? $array2[$i] : [];
        $array3[] = array_merge($part1, $part2);
    }