我有2个数组:
$array1 = array("dog","cat","butterfly") $array2 = array("dogs","bird","cows")
我需要从$array2得到所有部分匹配,比如$array1,如下所示:
array("dog","cat")
所以我需要检查$array2中是否存在部分字匹配,并使用$array1键和值输出新数组.
array_intersection只输出完整匹配
谢谢
解决方法
获得相同结果的更多方法
<?PHP $array1 = array("dog","butterfly"); $array2 = array("dogs","cows"); // Array output will be displayed from this array $result_array = $array1; // Array values are compared with each values of above array $search_array = $array2; print_r( array_filter($result_array,function($e) use ($search_array) { return preg_grep("/$e/",$search_array); })); ?>
产量
[akshay@localhost tmp]$PHP test.PHP Array ( [0] => dog [1] => cat )