2016-05-17 57 views
6

陣列1如何在PHP中連接數組值?

Array 
(
    [0] => Array 
     (
      [recomendation_id] => 3588 
      [employee_id] => 90141063 
      [attendance_type_id] => 2 
      [start_dtm] => 2016-05-17 10:32:00 
      [end_dtm] => 
      [request_message] => test notif 
      [recomendation_status_id] => 1 
      [last_update_dtm] => 2016-05-17 10:32:43 
      [employee_name] => Nike Yulistia Angreni 
      [attd_type_name] => Permittance 
      [status_name] => Request 
     ) 

) 

而且陣列2

Array 
(
    [0] => Array 
     (
      [valuator1] => Wulan Lastia Permana 
     ) 
) 

我想在一個數組的數組值。我想要這樣的結果:

Array 
(
    [0] => Array 
     (
      [recomendation_id] => 3588 
      [employee_id] => 90141063 
      [attendance_type_id] => 2 
      [start_dtm] => 2016-05-17 10:32:00 
      [end_dtm] => 
      [request_message] => test notif 
      [recomendation_status_id] => 1 
      [last_update_dtm] => 2016-05-17 10:32:43 
      [employee_name] => Nike Yulistia Angreni 
      [attd_type_name] => Permittance 
      [status_name] => Request 
      [valuator1] => Wulan Lastia Permana 
     ) 
) 

這樣可以加入嗎?

回答

9

你必須使用array_replace_recursive

<?php 

$arr1 = Array 
(
    Array 
     (
      "recomendation_id" => 3588, 
      "employee_id" => 90141063, 
      "attendance_type_id" => 2, 
      "start_dtm" => "2016-05-17 10:32:00", 
      "end_dtm" => "", 
      "request_message" => "test notif", 
      "recomendation_status_id" => 1, 
      "last_update_dtm" => "2016-05-17 10:32:43", 
      "employee_name" => "Nike Yulistia Angreni", 
      "attd_type_name" => "Permittance", 
      "status_name" => "Request" 
     ) 

); 
$arr2 = Array 
(
    Array 
     (
      "valuator1" => "Wulan Lastia Permana" 
     ) 
); 

print_r(array_replace_recursive($arr1,$arr2)); 

結果:

Array 
(
    [0] => Array 
     (
      [recomendation_id] => 3588 
      [employee_id] => 90141063 
      [attendance_type_id] => 2 
      [start_dtm] => 2016-05-17 10:32:00 
      [end_dtm] => 
      [request_message] => test notif 
      [recomendation_status_id] => 1 
      [last_update_dtm] => 2016-05-17 10:32:43 
      [employee_name] => Nike Yulistia Angreni 
      [attd_type_name] => Permittance 
      [status_name] => Request 
      [valuator1] => Wulan Lastia Permana 
     ) 

) 

Eval


如果使用array_merge_recursive,你output將是:

Array 
(
    [0] => Array 
     (
      [recomendation_id] => 3588 
      [employee_id] => 90141063 
      [attendance_type_id] => 2 
      [start_dtm] => 2016-05-17 10:32:00 
      [end_dtm] => 
      [request_message] => test notif 
      [recomendation_status_id] => 1 
      [last_update_dtm] => 2016-05-17 10:32:43 
      [employee_name] => Nike Yulistia Angreni 
      [attd_type_name] => Permittance 
      [status_name] => Request 
     ) 

    [1] => Array 
     (
      [valuator1] => Wulan Lastia Permana 
     ) 

) 
+0

有趣。我不知道這些功能。 'array_replace_recursive'不會覆蓋舊值嗎?還是它可以添加新的值? – Peter

+0

非常感謝!它完美的工作 –

+0

@Peter'array_replace_recursive'將取代舊值 – Thamilan

2

可以使用array_merge_recursive功能

$result = array_merge_recursive($array1, $array2); 
+3

上面的輸出與預期的不同。 – Ali

+0

'array_merge_recursive'會將值添加到不同的子數組中。 – Peter

0

是的,這是可能的。讓你的第一個數組是$arr1,第二個數組是$arr2

只需將一個新字段分配給您的第一個數組,您的第一個數組將更新第二個數組的key, value pair

$arr1[0]['valuator1'] = $arr2[0]['valuator1']; 

print_r($arr1); 

結果:

Array 
(
    [0] => Array 
     (
      [recomendation_id] => 3588 
      [employee_id] => 90141063 
      [attendance_type_id] => 2 
      [start_dtm] => 2016-05-17 10:32:00 
      [end_dtm] => 
      [request_message] => test notif 
      [recomendation_status_id] => 1 
      [last_update_dtm] => 2016-05-17 10:32:43 
      [employee_name] => Nike Yulistia Angreni 
      [attd_type_name] => Permittance 
      [status_name] => Request 
      [valuator1] => Wulan Lastia Permana 
     ) 

) 
0

這不是在我看來是最好的選擇,但它仍然會解決您的問題。

foreach ($arr2 as $k => $v) { 
    $arr1[$key] = array_merge($arr1[$key], $v); 
} 

這將遍歷第二個數組並將每個值與第一個數組合並。

注意:你的數組鍵在兩個數組中都應該是相同的,這樣才能工作。