I have 2 array, I want to compare them by matched values and order and count of total eligible values.
$a = [A,B,C,D,E,F,G];
| | |
$b = [B,A,C,D,F,E,G];
In this case the output should be 3 . How can I achieve this with top performance?
Update:
I am not asking matched values only , values should matched at the same order as well.
Array_diff_assoc will count what is not the same (4).
Count will count the number of items (7).
7-4 = 3.
echo count($a) - count(array_diff_assoc($a,$b)); // 3
Edit or just array_intersect_assoc
echo count(array_intersect_assoc($a,$b)); //3
Didn't cross my mind until now.
Assuming that both arrays have the same size you can do it with a simple loop:
$count = 0;
foreach ($a as $key => $value) {
if ($value === $b[$key]) {
$count++;
}
}
var_dump($count);
If they have different sizes then you must check if the key exists in the second array too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.