如何在2个数组之间进行字符串比较

时间:2013-07-26 10:45:04

标签: php arrays

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

3 个答案:

答案 0 :(得分: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
)

答案 1 :(得分:3)

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

$returned = array_intersect($array1, $array2);
echo count($returned);

答案 2 :(得分: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;