How to join array values ​​in PHP?

I have array 1 :

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

And array 2 :

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

I want to make array values ​​in one array. I want to get the result as follows:

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

Can I join in this way?

+6
source share
4 answers

You should use 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)); 

Result:

 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


If you use array_merge_recursive , your output will be:

 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 ) ) 
+9
source

You can use array_merge_recursive

 $result = array_merge_recursive($array1, $array2); 
+2
source

Yes it is possible. Let your first array be $arr1 and the second array be $arr2 .

Just assign a new field to your first array, your first array will be updated with a second array of key, value pair .

 $arr1[0]['valuator1'] = $arr2[0]['valuator1']; print_r($arr1); 

Result:

 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
source

This is not the best option, in my opinion, but it will still solve your problem.

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

This will go through the second array and combine each value with the first array.

Note. Array keys must be the same in both arrays for this to work.

0
source

All Articles