使用array_walk和闭包补全缺失键值对的问题排查
问题描述
从外部数据源获取的数据中,字段值为null时会直接丢弃该字段,而非传递null值。为避免后续流程报错,我尝试用array_walk结合闭包做预处理,但代码未达到预期效果。
原代码
<?php $lead = array ( 'contactId' => '63bda0xxxxx55feab6d671', 'contactPhone' => '9175550154', 'lastName' => 'Cxxx', 'email' => 'xxx@yahoo.com', 'lists' => array ( 0 => array ( 'id' => '62a3810xxxxxa8be2', 'name' => 'Unlimited5gHomeInternet', ), ), 'customFields' => array ( ), 'subscriptionStatus' => 'OPT_IN', 'created' => '2023-01-10T17:28:06.599Z', 'updated' => '2023-01-10T17:28:06.599Z', 'updateSource' => 'WEB_FORM', ); $expected_keys_1 = ['contactPhone', 'firstname', 'lastName', 'contactPhone','email']; $expected_keys_2 = ['zip_coode', 'address', 'city', 'state']; array_walk($expected_keys_1, function($value,$key) use (&$lead){ if(!isset($lead['$value'])){ $lead += array('$key' => null); } }); array_walk($expected_keys_2, function($value,$key) use (&$lead){ if(!isset($lead['customFields']['$value'])){$lead += array('$key' => null);} }); var_export($lead);
当前执行结果
array ( 'contactId' => '63bda026f895a55feab6d671', 'contactPhone' => '9178050154', 'firstName' => 'Juan', 'lastName' => 'Cueto', 'email' => 'wanchy@yahoo.com', 'lists' => array ( 0 => array ( 'id' => '62a381052b9f1f18ae8a8be2', 'name' => 'Unlimited5gHomeInternet', ), ), 'customFields' => array ( ), 'subscriptionStatus' => 'OPT_IN', 'created' => '2023-01-10T17:28:06.599Z', 'updated' => '2023-01-10T17:28:06.599Z', 'updateSource' => 'WEB_FORM', '$key' => NULL, )
期望结果
array ( 'contactId' => '63bda026f895a55feab6d671', 'contactPhone' => '9178050154', 'firstname' => NULL, 'lastName' => 'Cueto', 'email' => 'wanchy@yahoo.com', 'lists' => array ( 0 => array ( 'id' => '62a381052b9f1f18ae8a8be2', 'name' => 'Unlimited5gHomeInternet', ), ), 'customFields' => array ( 'zip_code' => NULL, 'address' => NULL, 'city' => NULL, 'state' => NULL, ), 'subscriptionStatus' => 'OPT_IN', 'created' => '2023-01-10T17:28:06.599Z', 'updated' => '2023-01-10T17:28:06.599Z', 'updateSource' => 'WEB_FORM', )
问题分析与修正代码
原代码错误点
- 字符串变量解析错误:
$lead['$value']把$value当作字符串字面量处理,而非变量;同时错误地用遍历的数组索引$key作为字段名,实际应该用数组元素值(目标字段名)。 - 字段添加位置错误:第二个循环中,应该往
customFields子数组添加字段,而非直接给外层$lead数组加键。 - 重复字段处理:
expected_keys_1中有重复的contactPhone,会导致不必要的重复检查。
修正后的代码
<?php $lead = array ( 'contactId' => '63bda0xxxxx55feab6d671', 'contactPhone' => '9175550154', 'lastName' => 'Cxxx', 'email' => 'xxx@yahoo.com', 'lists' => array ( 0 => array ( 'id' => '62a3810xxxxxa8be2', 'name' => 'Unlimited5gHomeInternet', ), ), 'customFields' => array ( ), 'subscriptionStatus' => 'OPT_IN', 'created' => '2023-01-10T17:28:06.599Z', 'updated' => '2023-01-10T17:28:06.599Z', 'updateSource' => 'WEB_FORM', ); // 去重避免重复处理相同字段 $expected_keys_1 = array_unique(['contactPhone', 'firstname', 'lastName', 'contactPhone','email']); $expected_keys_2 = ['zip_code', 'address', 'city', 'state']; array_walk($expected_keys_1, function($fieldName) use (&$lead){ // 直接使用变量作为数组键名,检查字段是否存在 if(!isset($lead[$fieldName])){ $lead[$fieldName] = null; } }); array_walk($expected_keys_2, function($fieldName) use (&$lead){ // 往customFields子数组添加缺失字段 if(!isset($lead['customFields'][$fieldName])){ $lead['customFields'][$fieldName] = null; } }); var_export($lead);
修正说明
- 移除了数组键名的引号包裹,直接使用遍历得到的字段名变量
$fieldName。 - 第二个循环修改为向
$lead['customFields']子数组添加字段,符合期望结果的结构。 - 对
expected_keys_1执行去重操作,避免重复检查同一字段,提升效率。
内容的提问来源于stack exchange,提问作者Jimbus
相关产品推荐
相关产品推荐

