0

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

Tonmoy
  • 2,008
  • 2
  • 10
  • 16
  • maybe you can try reading the files line by line? https://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php – Frederick Zhang Jun 16 '16 at 00:54
  • That's odd. One thing I noticed in your dump is that there are newlines with each element (see how the closing quotes are always on the next line). Try adding the flag [FILE_IGNORE_NEW_LINES](http://php.net/manual/en/function.file.php) to the second argument of your file() calls e.g. `file($files_for_matching[0], FILE_IGNORE_NEW_LINES)` and see if that changes anything. – Mikey Jun 16 '16 at 03:00
  • If your test files doesn't end with empty lines then @Mikey is right. Note that `'5' !== '5\n'` and `'10\n' !== '10'` – shudder Jun 16 '16 at 04:11
  • Hi Mikey, you must have sharp eyes. Thanks a lot. Thanks everyone for valuable answers and comments. – Tonmoy Jun 20 '16 at 00:40

1 Answers1

0

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.

Mikey
  • 6,728
  • 4
  • 22
  • 45