Compare

A: '; $b_key_html = 'Key field for B: '; } if(isset($_REQUEST['a_key_sel'])) $a_key_sel = $_REQUEST['a_key_sel']; if(isset($_REQUEST['b_key_sel'])) $b_key_sel = $_REQUEST['b_key_sel']; if((isset($_REQUEST['a']) && isset($_REQUEST['b'])) && ($_REQUEST['a'] !== '' && $_REQUEST['b'] !== '')) { $a = explode("\r\n", $_REQUEST['a']); if(!isset($a_key_sel)) $a_key_sel = null; $a_key = array_search($a_key_sel, $a_fields); $a = array_slice($a, 1); foreach($a as $line) { $bits = explode("\t", $line); if($bits[0] == '') continue; $a_keys[] = $bits[0]; foreach($bits as $bit) { $a_master[$bits[$a_key]][] = $bit; } } $b = explode("\r\n", $_REQUEST['b']); if(!isset($b_key_sel)) $b_key_sel = null; $b_key = array_search($b_key_sel, $b_fields); $b = array_slice($b, 1); foreach($b as $line) { $bits = explode("\t", $line); if($bits[0] == '') continue; $b_keys[] = $bits[0]; foreach($bits as $bit) { $b_master[$bits[$b_key]][] = $bit; } } } if(!isset($_REQUEST['a'])) $_REQUEST['a'] = null; if(!isset($_REQUEST['b'])) $_REQUEST['b'] = null; if(!isset($a_key_html)) $a_key_html = null; if(!isset($b_key_html)) $b_key_html = null; if($_REQUEST['a'] == null && $_REQUEST['b'] == null) { echo '

Paste data from two Excel files into the fields below and click Process. Note that the first row of data should consist of field names.

'; $button = ''; } else if($_REQUEST['a_key_sel'] == null && $_REQUEST['b_key_sel'] == null) { echo '

Next, select the key fields from each dataset and click Compare.

'; $button = ''; } else { echo '

Select one of the buttons below to display the desired comparison.

'; $button = '

Comparison options:

Display records within A that are not within B:
Display records within B that are not within A:
Display records present in both A and B:
'; } echo '
A'.$a_key_html.'
B'.$b_key_html.'
'.$button.'
'; $export = null; if(isset($_REQUEST['in_a_not_b'])) { echo '

Result

'; $result = @array_diff_inverse($a_keys, $b_keys); if(count($result) == 0 || !isset($a_master[current($result)])) { echo '

There are no records within A that are not within B for the specified keys.

'; exit; } echo '

Records within A that are not within B: (Export)

'; foreach($a_fields as $field) { echo ''; $export .= $field.','; } $export .= "\n"; echo ''; foreach($result as $match) { echo ''; foreach($a_master[$match] as $item) { echo ''; $export .= $item.','; } echo ''; $export .= "\n"; } echo '
'.$field.'
'.$item.'
'; $fn = md5(time()).'.csv'; $_SESSION['export'] = $fn; file_put_contents($fn, $export); } if(isset($_REQUEST['in_b_not_a'])) { echo '

Result

'; $result = @array_diff_inverse($b_keys, $a_keys); if(count($result) == 0 || !isset($b_master[current($result)])) { echo '

There are no records within B that are not within A for the specified keys.

'; exit; } echo '

Records within B that are not within A: (Export)

'; foreach($b_fields as $field) { echo ''; $export .= $field.','; } echo ''; $export .= "\n"; foreach($result as $match) { echo ''; foreach($b_master[$match] as $item) { echo ''; $export .= $item.','; } echo ''; $export .= "\n"; } echo '
'.$field.'
'.$item.'
'; $fn = md5(time()).'.csv'; $_SESSION['export'] = $fn; file_put_contents($fn, $export); } if(isset($_REQUEST['in_both'])) { echo '

Result

'; $result = @array_intersect($a_keys, $b_keys); if(count($result) == 0 || !isset($a_master[current($result)])) { echo '

All records from both datasets are unique for the specified keys.

'; exit; } echo '

Records present in both A and B: (Export)

'; foreach($a_fields as $field) { echo ''; $export .= $field.','; } echo ''; $export .= "\n"; foreach($result as $match) { echo ''; foreach($a_master[$match] as $item) { echo ''; $export .= $item.','; } echo ''; $export .= "\n"; } echo '
'.$field.'
'.$item.'
'; $fn = md5(time()).'.csv'; $_SESSION['export'] = $fn; file_put_contents($fn, $export); } ?>