jschofield Posted April 2, 2007 Share Posted April 2, 2007 Hello All, I have two text files. They both have an ID that is the same. One file has an ID and a phone number. The other file has the same ID, a different ID and extra information. What I am trying to do is link one file with the second file to create one file. Problem is I do not know how to do this. I am a beginner at php but any and all help would be great. My data from both files is below. PLEASE HELP. Thanks to all. Johnnie Data File 1: "RelationID","RelationsHomePhone" " 21","(619) 267-4262" " 22","(619) 470-0580" " 24","(619) 420-0625" Data File 2: "StudentsFamilyID","StudentID","StudentsFirstName","StudentsLastName","StudentsGradeLevel","UDFList3" " 21"," 25","Brittany","Allison"," 05","" " 22"," 26","Melodie","Alvarez"," 06","" " 24"," 29","Siana","Avitia"," 06","" Link to comment https://forums.phpfreaks.com/topic/45333-solved-link-two-txt-files-together-to-create-one/ Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 <?php $file1 = file_get_contents("file1.txt"); $file2 = file_get_contents("file2.txt"); $file1 = split("\n", $file1); $file2 = split("\n", $file2); $newfile = '"StudentsFamilyID","StudentID","StudentsFirstName","StudentsLastName","StudentsGradeLevel","UDFList3","RleationsHomePhone"' . "\n"; foreach ($file2 as $line) { list($id) = split(",", $line); foreach ($file1 as $line2) { list($id2, $phone) = split(",", $line2); if ($id == $id2) { $line = trim($line, "\r\n"); $newfile .= $line . "," . $phone . "\n"; break; } } } $fp = fopen("newfile.txt", "+w"); fwrite($fp, $newfile); fclose($fp); ?> give that a shot. Link to comment https://forums.phpfreaks.com/topic/45333-solved-link-two-txt-files-together-to-create-one/#findComment-220119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.