PHP多维数组正则匹配、合并及优先级处理技术咨询
PHP多维数组匹配:从基础需求到升级方案落地
初始需求实现
初始需求逻辑清晰:用$ct_house_options中的正则匹配$input_house_data,再和全量选项$ct_all_house_options合并,生成带available/not available状态标记的完整列表。
直接上实现代码:
// 1. 收集所有匹配成功的选项key $matchedKeys = []; foreach ($ct_house_options as $rule) { if (preg_match($rule['regex'], $input_house_data)) { $matchedKeys[$rule['key']] = true; } } // 2. 合并全量选项并标记状态 $finalHouseList = []; foreach ($ct_all_house_options as $key => $label) { $finalHouseList[] = [ 'key' => $key, 'label' => $label, 'status' => isset($matchedKeys[$key]) ? 'available' : 'not available' ]; }
升级需求:多规则匹配+优先级覆盖+性能优化
业务升级后需处理三种匹配规则(仅正则、正则+产品系列、正则+产品系列+编号),同时解决三个核心问题:合并全选项与匹配结果、优先级覆盖、性能优化。
标准化规则结构
先统一匹配规则的格式,每个规则需包含:key(选项唯一标识)、regex(匹配正则)、priority(优先级,数值越高优先级越高)、output(输出值,支持布尔/字符串),可选字段product_series和product_id用于精准匹配:
$productMatchRules = [ [ 'key' => 'PROD_A', 'regex' => '/^PRODUCT_A/', 'priority' => 1, 'output' => true ], [ 'key' => 'PROD_B', 'regex' => '/^PRODUCT_B/', 'product_series' => 'SERIES_01', 'priority' => 2, 'output' => 'SPECIFIC_VALUE_B' ], [ 'key' => 'PROD_C', 'regex' => '/^PRODUCT_C/', 'product_series' => 'SERIES_02', 'product_id' => 'ID_1001', 'priority' => 3, 'output' => false ] ];
核心问题解决思路
- 合并全选项与匹配结果:将全量选项转成键值对结构,匹配结果直接通过
key定位更新output字段,无需额外合并操作。 - 优先级覆盖:先按优先级从高到低排序规则,高优先级规则先执行匹配,成功后直接更新对应选项的
output,后续低优先级规则不会覆盖已设置的值。 - 性能优化:预编译正则避免重复编译;分层判断(先正则、再系列、最后编号)提前跳过无效规则;用键值对操作替代遍历查找,提升定位效率。
完整实现代码
function processProductMatches($inputData, $allOptions, $matchRules) { // 1. 按优先级降序排序规则,高优先级先执行 usort($matchRules, function($a, $b) { return $b['priority'] - $a['priority']; }); // 2. 预编译正则,过滤无效正则 foreach ($matchRules as &$rule) { $pattern = preg_replace('/\/$/', '', $rule['regex']); $rule['compiled_regex'] = @preg_match($pattern, '') === false ? false : $pattern . '/'; } unset($rule); // 3. 初始化全量选项,默认output为null $result = array_column($allOptions, null, 'key'); foreach ($result as &$item) { $item['output'] = null; } unset($item); // 4. 遍历规则执行匹配 foreach ($matchRules as $rule) { if (!$rule['compiled_regex']) continue; // 正则不匹配直接跳过 if (!preg_match($rule['compiled_regex'], $inputData['product_name'])) { continue; } // 产品系列匹配(规则指定才判断) if (isset($rule['product_series']) && $inputData['product_series'] !== $rule['product_series']) { continue; } // 产品编号匹配(规则指定才判断) if (isset($rule['product_id']) && $inputData['product_id'] !== $rule['product_id']) { continue; } // 匹配成功,更新output(高优先级已先执行,自动覆盖低优先级) $result[$rule['key']]['output'] = $rule['output']; } // 转成索引数组返回(按需调整) return array_values($result); } // 调用示例 $inputProductData = [ 'product_name' => 'PRODUCT_C_SERIES_02', 'product_series' => 'SERIES_02', 'product_id' => 'ID_1001' ]; $list_all_product_options_short = [ ['key' => 'PROD_A', 'label' => '产品A'], ['key' => 'PROD_B', 'label' => '产品B'], ['key' => 'PROD_C', 'label' => '产品C'] ]; $finalResult = processProductMatches($inputProductData, $list_all_product_options_short, $productMatchRules); print_r($finalResult);
关键细节说明
- 正则预编译:提前验证正则有效性,避免运行时报错,同时减少重复编译开销
- 分层判断:每一步不满足条件直接跳过,减少无效逻辑执行
- 键值对操作:通过
key直接定位选项,比遍历查找效率提升显著
内容的提问来源于stack exchange,提问作者Clipart - Designer
相关产品推荐
相关产品推荐

