匹配内爆数据与另一个内爆
问题描述:
I have data from two different tables where the data have been implode after being selected from Checkboxes and they are divided by a ','. After i selected them from the database the two varibles could look like this.
$firstvar = Red, Blue, Green, Yellow
$secondvar = Green, Purple, White
So i want to know how i can check, if there is a match, when there is at least one of the colors that match in the two variables.
i've been trying with:
if (strpos($firstvar , $firstvar ) !== false) {
echo 'There is a match';
}
But it doesn't work.
我从两个不同的表中获取数据,其中数据在从Checkbox中选中后已经内爆,并且它们被一个 ''。 从数据库中选择后,两个变量看起来像这样。 p>
$ firstvar =红色,蓝色,绿色,黄色
$ secondvar =绿色,紫色, White
code> pre>
所以我想知道如果匹配的话,我可以检查两个变量中是否至少有一种颜色匹配。 p>
我一直在尝试: p>
if(strpos($ firstvar,$ firstvar)!== false){
echo'有匹配';
}
code> pre>
但它不起作用。 p>
div>
答
After fetching the result from the database, explode these two variables:
Just like this:
$firstvar = explode(",",$firstvar);
$secondvar = explode(",",$secondvar );
Now, use this function:
$match = array_intersect($firstvar,$secondvar);
and now, implode the resulted output:
$result = implode(",",$match);
echo $result;
Hope, this may be useful to you.