为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
幕布斯6054654 2022-01-24 13:03:19
我有两个具有公共键的数组,employee_id我想将name原始数组中的列插入到第二个数组中。这是我从名为的数组变量中获得的内容: $resultArray ( [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_empArray ( [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 ) )
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

如果您使用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);


查看完整回答
反对 回复 2022-01-24
  • 1 回答
  • 0 关注
  • 248 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信