使用2个数组,检查有多少相同的元素

I have 2 arrays, example:

$array1 = array('hello1', 'hello2', 'hello3');
$array2 = array('world1', 'world2', 'hello3');

How can I count how many identical elements occur in these 2 arrays? So in this case, the value would be 1.

Perform an array_intersect() then call count() the number of elements:

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

Use the php built in functions when at all possible.

use array_intersect() function to get common values

Try this naïve code out:

$count = 0;
foreach($array1 as $val1) {
   foreach($array2 as $val2) {
      if ($val1 == $val2) {
         $count++;
      }
   }
}

I am not of PHP knowledgeable much. But looping through array is same as in other languages.

Edit: I have found the following way:

$difference = array_diff($array1,$array2);
$count = count($difference);