在php中将一个值从数组添加到另一个具有相同ID的数组

我有两个具有公共键的数组,employee_id我想将name原始数组中的列插入到第二个数组中。


这是我从名为的数组变量中获得的内容: $result


Array ( [0] => Array ( [employee_id] => 14100001 [calculate_id] => 26 [salary] => 696.35416666667 [late_duration] => 2 [undertime_duration] => 3 [cola_duration] => 0 [date] => 2019-09-09 )

[1] => Array ( [employee_id] => 14100002 [calculate_id] => 26 [salary] => 350 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 0 [date] => 2019-09-09 )

[2] => Array ( [employee_id] => 14100003 [calculate_id] => 26 [salary] => 358.75 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 2 [date] => 2019-09-09 )

[3] => Array ( [employee_id] => 14100004 [calculate_id] => 26 [salary] => 385 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 8 [date] => 2019-09-09 )

[4] => Array ( [employee_id] => 14100005 [calculate_id] => 26 [salary] => 380.625 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 7 [date] => 2019-09-09 ) ) 

这是我的另一个名为的数组变量: $this->data_emp


Array ( [0] => Array ( [employee_id] => 14100001 [name] => John Doe )

 [1] => Array ( [employee_id] => 14100002 [name] => John Dark )

 [2] => Array ( [employee_id] => 14100003 [name] => John Die )

 [3] => Array ( [employee_id] => 14100004 [name] => Liza Dark )

 [4] => Array ( [employee_id] => 14100005 [name] => Jane Dark )

 [5] => Array ( [employee_id] => 14100006 [name] => Elisse Dark ) )


幕布斯6054654
浏览 249回答 1
1回答

临摹微笑

如果您使用array_column重新索引by中的name值,则可以遍历,添加与中的每个值对应的:emp_dataemployee_id$resultnameemployee_id$result$emp_name_by_id = array_column($data_emp, 'name', 'employee_id');foreach ($result as &$r) {    $r['name'] = $emp_name_by_id[$r['employee_id']] ?? 'N/A';}print_r($result);
打开App,查看更多内容
随时随地看视频慕课网APP