@
Sunyanzi $b = [
'r1' => '/abc\d/',
'r2' => '/cbd\d/',
'r3' => '/def\d/'
];
我测试了@
LionXen的正则表达式,发现不能匹配。修改成这样才能匹配:
$b = array('r1' => '/abc/', 'b2' => '/cbd/', 'f5' => '/def/');
测试代码:
<?php
$a = array('abc7', 'cbd4', 'def9');
$b = array('r1' => '/abc/', 'b2' => '/cbd/', 'f5' => '/def/');
$result = array(); // Store the result
$iloop = 0; // Use to match corresponding element key in $a;
$matches = array();
foreach($b AS $key => $preg) {
if (preg_match($preg, $a[$iloop++], $matches)) {
$result[$key] = $matches[0];
}
}
print_r($result);
?>