2017-07-28 70 views
0

陣列我有含有2個陣列數據。我想能夠將它們添加在一起,以便第二個數組中的信息連接到第一個數組中。目前我正在做的array_merge將第二個數組添加到第一個數組的末尾。合併與現有的陣列的PHP

陣列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);但其將所述第二數組的第一個的端部。

任何想法?

+0

每個陣列中有2個陣列,因此與對口,而不是主陣列合併的每個那些陣列。 – WheatBeak

+0

你試過['array_merge_recursive()'](http://php.net/manual/en/function.array-merge-recursive.php)嗎? – axiac

回答

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

該obviou的輸出狡猾的是:

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:

在下面你問,如果這種方法可以推廣到合併數組任意數量的註釋。當然這是可能的,你只需要添加一次迭代層:

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

) 
+0

天才。如果我有兩個以上的數組,這是否適用?如果我想將3個數組合併成一個? –

+0

當然,我爲這個答案增加了一個變體作爲UPDATE。 – arkascha

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); 
+0

我試過這個,但它沒有工作,請參閱編輯的問題我得到的輸出與array_merge()和array_merge_recursive() –

+0

@IbrahimHafiji請參閱我的編輯 –

0

你想創建一個多維數組?

這是相當容易的。

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

試試你自己的代碼。你會發現它不會像問題中所描述的那樣產生輸出。 – arkascha

+1

你是對的,我領先於自己,並沒有完成這個問題。 –

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