0

我正在尝试在两个数组之间进行字符串比较,数组中填充了文本字符串。当我得到一个匹配项时,我想计算它并在最后输出最终数量,例如,如果两个数组每个有 100 个元素并且两个数组有 35 个匹配项,我希望输出说明这一点。提前致谢。

4

3 回答 3

3

您可以使用array_intersect找出两个数组的匹配元素。

 <?php
 $array1 = array("a" => "green", "red", "blue");
 $array2 = array("b" => "green", "yellow", "red");
 $result = array_intersect($array1, $array2);
 print_r($result);
 ?>

输出

Array
(
[a] => green
[0] => red
)
于 2013-07-26T10:48:07.417 回答
3

您可以使用array_intersect($array1, $array2);

$returned = array_intersect($array1, $array2);
echo count($returned);
于 2013-07-26T10:48:28.197 回答
0

你可以这样做

$data=array();
$count_matched=0;
$arr=array("abc","bcd","ghf","xxx","xcv","zsa");
$arr1=array("abc","bcd","ert","ccc","dfg","wer");
foreach($arr as $val)
{
    foreach($arr1 as $each)
    {
        if($val==$each)
        {
            $count_matched=$count_matched+1;
            break;
        }
    }
}
$data['match_count']=$count_matched;
echo "<pre>";print_r($data);die;
于 2013-07-26T10:54:58.267 回答