2017-07-25 66 views
0

目前,我有2個多維數組,我正在尋找將它們組合成一個巨型數組,其中數組1中的值名稱與數組2中的值名稱匹配。 數組的外觀作爲跟隨...PHP - 基於值的合併2多維數組

Array1 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => test1 
      [desc] => test_desc 
      [quantity] => 3 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [name] => test2 
      [desc] => test_desc 
      [quantity] => 33 
     ) 

) 


Array2 
(
    [0] => Array 
     (
      [holder] => 'John' 
      [name] => test1 
      [desc] => test_desc 
      [location] => ATL 
     ) 

    [1] => Array 
     (
      [holder] => 'Jackie' 
      [name] => test3 
      [desc] => test_desc 
      [location] => SF 
     ) 

) 

我正在尋找合併其中陣列1的「名稱」列數組2相匹配的陣列以及在陣列的1 & 2相結合的列到最後一個數組。這看起來像...

FinalArray 
(
    [0] => Array 
     (
      [id] => 1 
      [holder] => 'John' 
      [name] => test1 
      [desc] => test_desc 
      [location] => ATL 
      [quantity] => 3 
     ) 

    [1] => Array 
     (
      [holder] => 'Jackie' 
      [name] => test3 
      [desc] => test_desc 
      [location] => SF 
     ) 

    [2] => Array 
     (
      [id] => 2 
      [name] => test2 
      [desc] => test_desc 
      [quantity] => 33 
     ) 

) 

凡「測試1」融合了跨2個陣列的不同列到裏面的「FinalArray」一個新的數組。我試着用array_merge和array_merge_recursive來研究一些想法,但我不完全確定我是否會朝着正確的方向前進。提前致謝。

+1

讓我們瞭解您到目前爲止 – RiggsFolly

回答

1

嘗試這樣

$array1=[['id' => 1,'name' => 'test1','desc' => 'test_desc','quantity' => 3], 
    ['id' => 2,'name' => 'test2','desc' => 'test_desc','quantity' => 33]]; 
$array2=[['holder' => 'John','name' => 'test1','desc' => 'test_desc','location' => 'ATL'], 
    ['holder' => 'Jackie','name' => 'test3','desc' => 'test_desc','location' => 'SF']]; 
$final=[]; 
foreach ($array1 as $key1=>$data1){ 
    foreach ($array2 as $key2=>$data2){ 
     if($data1['name']==$data2['name']){ 
      $final[]=$data1+$data2; 
      unset($array1[$key1]); 
      unset($array2[$key2]); 
     } 
    } 
} 
if(!empty($array1)){ 
    foreach ($array1 as $value){ 
     $final[]=$value; 
    } 
} 
if(!empty($array2)){ 
    foreach ($array2 as $value){ 
     $final[]=$value; 
    } 
} 

它會給輸出

enter image description here

+0

試圖@msleone試試這個答案。 –

1

還有一個解決方案

function merge_by_name(array $arr1, array $arr2) { 
    $result = []; 

    foreach ($arr1 as $value) { 

     $key = array_search($value['name'], array_column($arr2, 'name')); 

     if($key !== false) { 
      $result[] = array_merge($value, $arr2[$key]); 
      unset($arr2[$key]); 
     } else { 
      $result[] = $value; 
     } 
    } 
    $result = array_merge($result, $arr2); 

    return $result; 
} 

測試

$arr1 = [ 
    [ 
     'id' => 1, 
     'name' => 'test1', 
     'desc' => 'test_desc', 
     'quantity' => 3 
    ], 
    [ 
     'id' => 2, 
     'name' => 'test2', 
     'desc' => 'test_desc', 
     'quantity' => 33 
    ], 
]; 

$arr2 = [ 
    [ 
     'holder' => 'John', 
     'name' => 'test1', 
     'desc' => 'test_desc', 
     'location' => 'ATL' 
    ], 
    [ 
     'holder' => 'Jackie', 
     'name' => 'test3', 
     'desc' => 'test_desc', 
     'location' => 'SF' 
    ], 
]; 

var_export(merge_by_name($arr1, $arr2)); 

結果

array (
    0 => 
    array (
    'id' => 1, 
    'name' => 'test1', 
    'desc' => 'test_desc', 
    'quantity' => 3, 
    'holder' => 'John', 
    'location' => 'ATL', 
), 
    1 => 
    array (
    'id' => 2, 
    'name' => 'test2', 
    'desc' => 'test_desc', 
    'quantity' => 33, 
), 
    2 => 
    array (
    'holder' => 'Jackie', 
    'name' => 'test3', 
    'desc' => 'test_desc', 
    'location' => 'SF', 
), 
)