0

I have 2 arrays with different number of elements, the case is, I want get show the same element have the 2 arrays.

For Example :

$search_terms="house,car,boy,table";
$cats="1,2,3,4,car,boy,fly,girl";

$explode_term=explode(",",$search_terms);
$explode_tags=explode(",",$cats);   

$compare=array_diff_assoc($explode_term, $explode_tags);

foreach ($compare as $compa)
{

    print $compa;
    print "<br>";   

}

As you can see I have 2 arrays and only have some elements in common, by this I want to get the element it´s the same in both cases.

4

2 回答 2

5

您正在寻找array_intersect.

array_intersect() 返回一个数组,其中包含所有参数中存在的 array1 的所有值。请注意,密钥是保留的。

array_intersect($explode_term, $explode_tags);
于 2014-04-05T21:36:29.053 回答
0
$search_terms="house,car,boy,table";
$cats="1,2,3,4,car,boy,fly,girl";

$explode_term=explode(",",$search_terms);
$explode_tags=explode(",",$cats);   

$compare=array_diff_assoc($explode_term, $explode_tags);

foreach ($compare as $compa)
{
    print $compa;
    print "<br>";   
}
于 2014-04-05T21:45:14.920 回答