需要一个解决方案来比较4个PHP数组并获得最终匹配字符串

As part of a project, my PHP code compares four text files and uses the output for next work. The text files contain phone numbers only separated by new line.

I've used file() function to get text file data in arrays and then array_intersect() function for matching data from arrays. But the output does not seem correct matching data.

//$files_for_matching is an array holding four file names

$matches1 = array_intersect(file($files_for_matching[0]), file($files_for_matching[1]));
$matches2 = array_intersect(file($files_for_matching[2]), file($files_for_matching[3]));
$final_matches = array_intersect($matches1, $matches2);

Here if I test with four dummy text files where textfile01 contains (7, 3, 6, 2, 13, 10, 5), textfile02 contains (1, 3, 9, 5, 7, 10), textfile03 contains (1, 3, 199, 5, 27, 10), textfile04 contains (11, 23, 1, 5, 3, 10) numbers in each.

var_dump($matches1);
var_dump($matches2);
var_dump($final_matches);

shows:

array (size=2)
  0 => string '7
' (length=3)
  1 => string '3
' (length=3)
array (size=4)
  0 => string '1
' (length=3)
  1 => string '3
' (length=3)
  3 => string '5
' (length=3)
  5 => string '10' (length=2)
array (size=1)
  1 => string '3
' (length=3)

So, as the final output it shows 3 as the only final matched data over the four text files where that is not true. I've tried with foreach loops over arrays and preg_match() funtion to match with each element of arrays to find the final matches and output comes same. So, I believe the problem is not in the code but in the solution. If somebody could show me a better way of solving this, I would be helpful. Thanks

Based on what you wrote, your original code should work.

You could try this:

$files_for_matching = [
   [7, 3, 6, 2, 13, 10, 5],
   [1, 3, 9, 5, 7, 10], 
   [1, 3, 199, 5, 27, 10], 
   [11, 23, 1, 5, 3, 10]
   // you can add more files
];

// $final_matches = array_intersect($files_for_matching[0], $files_for_matching[1], $files_for_matching[2], $files_for_matching[3]);

// better: applying array_intersect() on an unknown number of arrays
$final_matches = call_user_func_array('array_intersect', $files_for_matching);

print_r($final_matches); // [3, 10, 5]

Side-note: array_intersect() can take 2 or more arrays.