PHP中按ID匹配从次要数组向主数组添加属性
数组合并需求
主数组 $allcustomers 结构
array:4 [▼ 0 => array:13 [▼ "id" => 4 "name" => "shahd" "gender" => "F" "helper_code" => null "age" => 255.0 "dob" => 1768.0 "mobile_no" => "01789654123" "category" => null "status" => 0 "created_by" => 8 "updated_by" => null "created_at" => "2023-08-14T18:56:26.000000Z" "updated_at" => null ] 1 => array:13 [▼ "id" => 2 "name" => "amr" "gender" => "M" "helper_code" => 252 "age" => 25.0 "dob" => 1998.0 "mobile_no" => "01252523987" "category" => "Mixed" "status" => 47 "created_by" => 8 "updated_by" => 8 "created_at" => "2023-08-10T08:05:00.000000Z" "updated_at" => "2023-08-10T19:35:00.000000Z" ] 2 => array:13 [▼ "id" => 1 "name" => "mohamed shaker" "gender" => "M" "helper_code" => 2525 "age" => 2.0 "dob" => 2021.0 "mobile_no" => "01999999999" "category" => "Mixed" "status" => 9 "created_by" => 8 "updated_by" => 8 "created_at" => "2023-08-01T09:51:51.000000Z" "updated_at" => "2023-08-10T10:57:43.000000Z" ] 3 => array:13 [▼ "id" => 3 "name" => "tasnim" "gender" => "F" "helper_code" => 255 "age" => 25.0 "dob" => 1998.0 "mobile_no" => "01252525256" "category" => null "status" => 0 "created_by" => 8 "updated_by" => null "created_at" => "2023-07-11T19:13:55.000000Z" "updated_at" => null ] ]
次要数组 $lastvisitdate 结构
array:2 [▼ 0 => array:2 [▼ "id" => 1 "date" => "2023-08-28" ] 1 => array:2 [▼ "id" => 2 "date" => "2023-08-10" ] ]
需求说明
根据ID匹配,将次要数组中的date字段添加到主数组中。若主数组中的ID在次要数组中不存在,则对应的date字段设为null。
期望结果
array:4 [▼ 0 => array:13 [▼ "id" => 4 "name" => "shahd" "gender" => "F" "helper_code" => null "age" => 255.0 "dob" => 1768.0 "mobile_no" => "01789654123" "category" => null "status" => 0 "created_by" => 8 "updated_by" => null "created_at" => "2023-08-14T18:56:26.000000Z" "updated_at" => null "date" => null ] 1 => array:13 [▼ "id" => 2 "name" => "amr" "gender" => "M" "helper_code" => 252 "age" => 25.0 "dob" => 1998.0 "mobile_no" => "01252523987" "category" => "Mixed" "status" => 47 "created_by" => 8 "updated_by" => 8 "created_at" => "2023-08-10T08:05:00.000000Z" "updated_at" => "2023-08-10T19:35:00.000000Z" "date" => "2023-08-10" ] 2 => array:13 [▼ "id" => 1 "name" => "mohamed shaker" "gender" => "M" "helper_code" => 2525 "age" => 2.0 "dob" => 2021.0 "mobile_no" => "01999999999" "category" => "Mixed" "status" => 9 "created_by" => 8 "updated_by" => 8 "created_at" => "2023-08-01T09:51:51.000000Z" "updated_at" => "2023-08-10T10:57:43.000000Z" "date" => "2023-08-28" ] 3 => array:13 [▼ "id" => 3 "name" => "tasnim" "gender" => "F" "helper_code" => 255 "age" => 25.0 "dob" => 1998.0 "mobile_no" => "01252525256" "category" => null "status" => 0 "created_by" => 8 "updated_by" => null "created_at" => "2023-07-11T19:13:55.000000Z" "updated_at" => null "date" => null ] ]
解决方案
实现步骤
- 将次要数组转换为以
id为键、date为值的关联数组,实现O(1)时间复杂度的快速查找。 - 遍历主数组,为每个元素添加
date字段,值为关联数组中对应id的值(存在则取,否则设为null)。
示例代码(PHP)
// 将次要数组转换为id => date的关联数组 $dateMap = array_column($lastvisitdate, 'date', 'id'); // 遍历主数组添加date字段 foreach ($allcustomers as &$customer) { $customer['date'] = $dateMap[$customer['id']] ?? null; } unset($customer); // 解除引用,避免后续操作影响原数组 // 输出结果 print_r($allcustomers);
代码说明
array_column($lastvisitdate, 'date', 'id'):高效转换数组结构,把id作为键,date作为对应值,大幅提升查找效率。$dateMap[$customer['id']] ?? null:使用PHP的null合并运算符,简洁处理存在/不存在的情况,避免报错。- 遍历主数组时使用引用
&$customer,直接修改原数组元素,无需额外创建新数组。
内容的提问来源于stack exchange,提问作者Mohamed shaker
相关产品推荐
相关产品推荐

