我有两个数组即
$ar1=array("Mobile","shop","software","hardware");
和 $arr2=arry("shop","Mobile","shop","software","shop")我想比较 arr2 和 arr1 的元素,即
foreach($arr2 as $val) { if(in_array($val, $arr1)) { //mycode to insert data into mysql table variable++; // here there should be a variable that must be increamented when ever match found in $arr2 so that it can be saved into another table. } $query="update table set shop='$variable',Mobile='$variable'......."; }
$variable 应该是一个整数值,以便以后使用我可以使用这个变量(shop 即在这个例子中它的值应该是 3)来找到匹配项。
我的问题是如何获得每次找到匹配项时都会增加的变量。
最佳答案
抱歉,我不完全理解您的代码的用途。您可以使用 array_intersect获得共同值(value)观和array_diff比较两个数组时获取唯一值。
关于php - 如何比较两个数组并找到匹配元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5456028/