Jump to content

[SOLVED] Link two txt files together to create one


jschofield

Recommended Posts

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",""

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.