How do I merge 2 files that contain duplicates in PHP

Discussion in 'PHP' started by kensai, Oct 6, 2010.

  1. #1
    Hi everybody,
    I am looking for a way in PHP to merge 2 files that have duplicate values in their content and create a 3rd file that has only the unique values from both the previous files. More specific, I want to have all the values in a third file without a value appearing twice anywhere in the final file:

    These are the files:

    File1:
    +
    File2:
    =
    File3:

    Any help would be greatly appreciated.
     
    kensai, Oct 6, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    If the format is same, then you know on which line of the file 1 & 2 have same data.

    Use file() function of php to read file into array comprising of each line. Concat strings from both the arrays at given line numbers.

    If the files are dynamic in nature, then you need identifier to match the similar data.

    if you can share sample data of real file, may be we can give better suggestions.
     
    mastermunj, Oct 7, 2010 IP
  3. kensai

    kensai Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Hi Mastermunj,
    The files have the same format but with a mix of auto generated content in them, so they might differ in various places or might have the order of FIELDn changed.. FIELD1 could be on the first line on one file and on the 10th line on the second file.

    Consider the files to have the same layout as the samples given above.

    How can i start working with them, any sample code would be great..

    Thank you for helping me in this
    .BR
     
    kensai, Oct 7, 2010 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    okay,
    if it is ensured that actual data is below the FIELD name in the file, you need following:

    1. either identifier for field, or field list to know where in file fields are located.
    2. Read files, and prepare array of fields with their respective data into array.
    3. Do array looping and merge the content.

    Little challenging part above would be 2nd step, but it can be made simpler with better definition / identification of fields.
     
    mastermunj, Oct 7, 2010 IP
  5. kensai

    kensai Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Can you provide a sample code on how to do that? Anyone?
    Thank you all
     
    kensai, Oct 7, 2010 IP