merge files

Posted on 2012-08-17
Last Modified: 2012-08-18
Hi experts,

I need to merge 2 or more files. No matter the content the script should output lines from 2 or more files into one file.txt, randomized.

Question by:gloriaewold41
    LVL 25

    Expert Comment

    To merge known files ... this script will merge them into one, just place the file names into the $files array.  It does not randomize the contents however ... is that important?
    echo '<pre>';
    // old files to be merged
    $files   = array('/Apps/DATA/old_file_1.txt', '/Apps/DATA/old_file_2.txt');
    // new file name
    $newfile = '/Apps/DATA/new_file.txt';
    // delete file if already exists
    foreach ( $files as $f ) {
        // Open the file to get existing content
        $data = file_get_contents($f);
        // Append the contents back to the new file
        file_put_contents($newfile, $data, FILE_APPEND);    
    echo "done";

    Open in new window

    LVL 42

    Accepted Solution

    Have a look at this, and ask if you get any probs..

    //read each file into an array.
    $file1 = file('input1.txt');
    $file2 = file('input2.txt');
    //merge the two into one array
    $allFiles = array_merge($file1, $file2);
    //shuffle (randomize) the array
    //create or open the file to write to
    $output = fopen("output.txt", 'w') or die("can't open file");
    //loop through the lines and write them to an output file
    foreach ($allFiles as $line) {
    	fwrite($output, $line);
    //close the file

    Open in new window


    Author Comment

    I need output data randomized.
    LVL 42

    Expert Comment

    by:Chris Stanyon
    My code randomizes the output - it shuffles (randomizes) the lines before writing them to the output file!

    Author Closing Comment

    Works great. Thanks

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now