leke Posted November 29, 2011 Share Posted November 29, 2011 I'm trying to compare 2 files, each with data separated by ::. I'm trying to make a comparison that if the data to the left of the separator from one file is not found in the same column of the other file, then it is copied to an array. For example: file1.txt abc::def ggg::hhh mmm::ppp file2.txt abc::def zzz::aaa bbb::ccc So... ggg::hhh mmm::ppp is copied from file1.txt because ggg and mmm was not found on the same side in file2.txt ...but I'm not getting any error message with this. The operation just hangs. <?php $dic1 = file('file1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $dic2 = file('file2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $titleText = array(); // Storing results into this array. foreach ($dic1 as $dic1line){ list($dic1field1, $dic1field2) = explode('::', $dic1line); foreach ($dic2 as $dic2line){ list($dic2field1, $dic2field2) = explode('::', $dic2line); if ($dic1field1 != $dic2field1) { // Found a non match in DB. array_push($titleText, "$dic1field1 :: $dic1field2\n"); // Store all finds in array. } } } // Finish outputting anything left over. if (empty($titleText)) { // $value was not found -- array is empty. echo 'All matched, or something else.'; } else { $arrayOut = implode("", $titleText); // output the results found in the search. echo $arrayOut; } unset($value); ?> Anyone know how to do this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/252067-copying-entries-not-found-in-file/ Share on other sites More sharing options...
sunfighter Posted November 30, 2011 Share Posted November 30, 2011 The double colon is the Scope Resolution Operator http://php.net/manual/en/keyword.paamayim-nekudotayim.php Did you mean to use it that way? Quote Link to comment https://forums.phpfreaks.com/topic/252067-copying-entries-not-found-in-file/#findComment-1292699 Share on other sites More sharing options...
leke Posted November 30, 2011 Author Share Posted November 30, 2011 Quote The double colon is the Scope Resolution Operator http://php.net/manual/en/keyword.paamayim-nekudotayim.php Did you mean to use it that way? Nope. I used it as a delimiter in my text file db. I've still been working on the code. I have it down a bit better now... <?php $txt1 = file('txt1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $txt2 = file('txt2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $storage1 = array(); $storage2 = array(); foreach ($txt1 as $line){ array_push($storage1, list($part1, $part2) = explode('::', $line)); } foreach ($txt2 as $line){ array_push($storage2, list($part1, $part2) = explode('::', $line)); } ?> ...I just have to figure out how to check every $storage1[v][0] against $storage2[v][0] for equality. Quote Link to comment https://forums.phpfreaks.com/topic/252067-copying-entries-not-found-in-file/#findComment-1292753 Share on other sites More sharing options...
requinix Posted November 30, 2011 Share Posted November 30, 2011 PHP has a bunch of useful array functions, such as array_diff_key. Try getting all the data into a set of associative arrays: $dic1 = file('file1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $dic2 = file('file2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $titleText = array(); // Storing results into this array. $dic1assoc = array(); foreach ($dic1 as $line) { list($f1, $f2) = explode("::", $line); $dic1assoc[$f1] = $f2; } $dic2assoc = array(); foreach ($dic2 as $line) { list($f1, $f2) = explode("::", $line); $dic2assoc[$f1] = $f2; } $onenottwo = array_diff_key($dic1assoc, $dic2assoc); foreach ($onenottwo as $key => $value) { $titleText[] = $key . "::" . $value; } Quote Link to comment https://forums.phpfreaks.com/topic/252067-copying-entries-not-found-in-file/#findComment-1292758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.