Need help updating PHP script that counts duplicate emails in 2 uploaded CSV files

I've been asked to revise to a custom PHP script that I developed for someone (about a year ago) that counts duplicate email addresses that are in 2 uploaded CSV files.    

Right now, if there are duplicates in list #1, it reports those dupes.  What I need to do is update it so that it only reports the duplicates that are BETWEEN the two lists - and NOT the duplicates that are within either list.  

I thought that it'd be quick and easy, but I can't seem to figure it out. I'm thinking there might some way to filter the dupes out of each of the CSV files individually before I merge them, perhaps?

My PHP code and is below, and some sample CSV files are attached.  Any help would be appreciated.

Thanks!
- Yvan


<?php		
	
	$csv1filename = $_FILES['csv1']['tmp_name'];

	$ext1 = strtoupper(pathinfo($_FILES['csv1']['name'], PATHINFO_EXTENSION));							
	
	$csv2filename = $_FILES['csv2']['tmp_name'];
	
	$ext2 = strtoupper(pathinfo($_FILES['csv2']['name'], PATHINFO_EXTENSION));		

	if ($ext1 == 'CSV' && $ext2 == 'CSV')	{	

		$csv1 = '/home/expert/public_html/dupes/csv1.csv';					

		move_uploaded_file($csv1filename, $csv1);

		$csv2 = '/home/expert/public_html/dupes/csv2.csv';	

		move_uploaded_file($csv2filename, $csv2);					

		// CREATE THE EMPTY ARRAY
		
		$raw_array = array();				
		
		$csv1 = file($csv1, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES);
		
		$csv2 = file($csv2, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES);

		$raw_array = array_merge($csv1, $csv2); 					
		
		// FUNCTION TO COUNT DUPLICATE EMAILS IN THE ARRAY
		
		function array_not_unique($raw) {
			$new = array_count_values($raw);				
			foreach ($new as $key => $val) {
			   if ($val < 2) unset($new[$key]);
			}				
			return $new;				
		}		

		$common = array_not_unique($raw_array);

		// DELETE EXPORT CSV FILE IF IT ALREADY EXISTS	

		if ( file_exists("/home/expert/public_html/dupes/duplicates.csv") ) {
			unlink ("/home/expert/public_html/dupes/duplicates.csv");
		}	
		
		// OPEN FILE FOR WRITING AND ADD COLUMN HEADERS
		
		$fd = fopen("/home/expert/public_html/dupes/duplicates.csv", "a");
		
		fwrite($fd, "EMAIL\n");				

		// DISPLAY THE NUMBER OF DUPES FOUND IN THE ARRAY
		
		$total = 0;
		
		echo "<pre style='text-align:left;line-height:45px;'>";					
		
		foreach ($common as $x => $n) {
			
			$total++;							
			
			// LOOP THROUGH DATA AND APPEND DUPLICATE EMAILS TO CSV FILE					

			fwrite($fd, $x . "\n");	
			
		}	
		
		// CLOSE THE FILE
		
		fclose($fd);
		
		echo "</pre>";
		
		echo "<br />A Total of <strong style='text-align:center;background-color:#FBCB45;padding:3px;'>$total</strong> duplicate email addresses were found.<br /><br /><br />";
		
		echo "Click <strong><a href=\"duplicates.csv\" target=\_blank\">here</a></strong> to download a CSV file which contains<br />the duplicate email addresses<br /><br /><br />";	
						
		echo '<a href="/dupes/" style="font-size:18px;text-decoration:none;font-weight:bold;color:blue;">&lt;&lt; Back</a>';
		
		unlink('/home/expert/public_html/dupes/csv1.csv');
		
		unlink('/home/expert/public_html/dupes/csv2.csv');							

	} else {

		echo 'Improper file type uploaded.';
		
	}	
	
?>	

Open in new window

sample1.csv
sample2.csv
egoselfaxisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

egoselfaxisAuthor Commented:
Hmm .. did I just figure this out?  It appears so .. and seems to be working:

$csv1 = [b]array_unique[/b](file($csv1, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES));				
$csv2 = [b]array_unique[/b](file($csv2, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES));  

Open in new window


- Yvan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
greetings egoselfaxis, , , PHP is a programming language based on Arrays, and if you take the time to really GET to know the array functions -
      http://php.net/manual/en/ref.array.php
you can be a step ahead of yourself in PHP.

Glad you found the  array_unique( )
Ray PaseurCommented:
Got a Neglected Question Alert on this one.  Do you still want a hand or shall we close it out?  Please let us know!  Thanks, ~Ray
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Ray PaseurCommented:
Not sure if you're still needing anything here, and I've got to leave soon, so here are some ways of looking at arrays.   HTH, ~Ray
http://iconoun.com/demo/temp_egoselfaxis.php
<?php // demo/temp_egoselfaxis.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28645139.html
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT E-E
$urls[] = 'http://filedb.experts-exchange.com/incoming/2015/03_w13/905490/sample1.csv';
$urls[] = 'http://filedb.experts-exchange.com/incoming/2015/03_w13/905491/sample2.csv';

$all= [];
foreach ($urls as $url) {
    echo PHP_EOL . $url;
    $csv = file($url, FILE_IGNORE_NEW_LINES);
    $all = array_merge($all, $csv);
    $arr = [];
    foreach ($csv as $email) {
	    if (array_key_exists($email, $arr)) {
	        $arr[$email]++;
	    } else {
	        $arr[$email] = 1;
	    }
	}
	foreach ($arr as $e => $n) {
	    echo PHP_EOL . "EMAIL $e OCCURS $n TIMES";
	}
	echo PHP_EOL;
}
echo PHP_EOL;
$all = array_unique($all);
print_r($all);

Open in new window

egoselfaxisAuthor Commented:
Sorry guys -- my client went awol on this one.  I'm just going to close this out and award some points.

Cheers,
- Yvan
egoselfaxisAuthor Commented:
(My solution worked)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.