简体   繁体   English

将数组与现有数组php合并

[英]Merge an array with an existing array php

I have two array both containing data. 我有两个都包含数据的数组。 I want to be able to add them together so that the information from the second array joins into the first array. 我希望能够将它们添加在一起,以便第二个数组中的信息加入第一个数组中。 Currently the array_merge that I am doing adds the second array to the end of the first one. 目前,我正在执行的array_merge将第二个数组添加到第一个数组的末尾。

Array 1 阵列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
        )

Array 2 阵列2

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

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

        )

How can I Merge the two arrays so the output becomes like below? 如何合并两个数组,使输出如下所示?

Array 3 阵列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
        )

What is currently happening 目前正在发生什么

[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
        )

I have tried array_merge( $array1 , $array2 ); 我已经尝试了array_merge( $array1 , $array2 ); but that adds the second array to the end of the first one. 但这会将第二个数组添加到第一个数组的末尾。

Any ideas? 有任何想法吗?

This looks pretty forward to me, you simply array_merge() the elements of those two arrays: 这对我来说很期待,您只需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);

The output of that obviously is: 该输出显然是:

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
        )
)

UPDATE: 更新:

In the comment below you ask if this approach can be generalized to merge an arbitrary number of arrays. 在下面的评论中,您询问是否可以推广这种方法以合并任意数量的数组。 Sure that is possible, you just add another iteration layer: 当然可以,您只需添加另一个迭代层即可:

<?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);

This variant produces that output: 此变量产生该输出:

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
        )

)

What about this with array_merge_recursive ? 那么array_merge_recursive呢?

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. array_merge_recursive()将一个或多个数组的元素合并在一起,以便将一个数组的值附加到上一个数组的末尾。 It returns the resulting array. 它返回结果数组。

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

EDIT: If array_merge_recursive doesn't work for you then try this https://eval.in/838950 编辑:如果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);

You want to create a multi dimensional array? 您想创建一个多维数组吗?

That's pretty easy. 那很容易。

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

you can try this approach: 您可以尝试以下方法:

    $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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM