我有两个这样的数组:
$arr1 = array('/^under.*/','/^develop.*/','/^repons*/');
$arr2 = array('understand','underconstruction','developer','develope','hide','here','some')
我想匹配两个数组并返回一个以 $arr1
中的模式开头的单词数组。
如何在 PHP 中执行此操作?
最佳答案
这应该适合你:
<?php
$arr1 = array('/^under.*/','/^develop.*/','/^repons*/');
$arr2 = array('understand','underconstruction','developer','develope','hide','here','some');
$result = array();
foreach($arr1 as $pattern) {
foreach($arr2 as $value) {
if(preg_match_all($pattern, $value, $matches))
$result[] = $matches[0][0];
}
}
print_r($result);
?>
输出:
Array ( [0] => understand [1] => underconstruction [2] => developer [3] => develope )
关于php - 比较两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801776/