比较两个数组

时间:2015-01-06 15:26:29

标签: php

我有两个这样的数组:

$arr1 = array('/^under.*/','/^develop.*/','/^repons*/');
$arr2 = array('understand','underconstruction','developer','develope','hide','here','some')

我希望匹配两个数组并返回以$arr1中的模式开头的单词数组。

我如何在php中执行此操作?

1 个答案:

答案 0 :(得分:2)

这应该适合你:

<?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 )