PHP - explode values in an array, output to tab separated file

ictl picture ictl · Jul 4, 2013 · Viewed 12.9k times · Source

I have the following in a file called test_tab.txt (tab separated):

header1 header2 header3
field1  field1a field1b; field1c
field2  field2a field2b
field3  field3a 
field4  field4a field4b; field4c; field4d; field4e
field5  field5a 
field6  field6a field6b; field6c

I would like to convert that to the following (also tab separated, written to another text file):

header1 header2 header3
field1  field1a field1b
field1  field1a field1c
field2  field2a field2b
field3  field3a 
field4  field4a field4b
field4  field4a field4c
field4  field4a field4d
field4  field4a field4e
field5  field5a 
field6  field6a field6c
field6  field6a field6c

I'm new to programming in general, but I've googled around and I think a possible way of doing this would be to create an array and somehow explode the header3 key-value pair. I'm not sure how to go about copying the fields under header1 and header2 though.

All I have at the moment is some code to create an array:

$mainarray = array(); 
foreach (file('test_tab.txt') as $row) { 
  list($header1, $header2, $header3) = explode("\t", $row); 
  $mainarray[] = array(header1 => $header1, header2 => $header2, header3 => $header3); 
} 

print_r($mainarray);

which gives:

    Array
    (
        [0] => Array
            (
                [header1] => header1
                [header2] => header2
                [header3] => header3

            )

        [1] => Array
            (
                [header1] => field1
                [header2] => field1a
                [header3] => field1b; field1c

            )

        [2] => Array
            (
                [header1] => field2
                [header2] => field2a
                [header3] => field2b

            )

        [3] => Array
            (
                [header1] => field3
                [header2] => field3a
                [header3] => 

            )

        [4] => Array
            (
                [header1] => field4
                [header2] => field4a
                [header3] => field4b; field4c; field4d; field4e

            )

        [5] => Array
            (
                [header1] => field5
                [header2] => field5a
                [header3] => 

            )

        [6] => Array
            (
                [header1] => field6
                [header2] => field6a
                [header3] => field6b; field6c
            )

    )

Any ideas on how to approach this? I'm using PHP version 5.3.10.

Answer

web-nomad picture web-nomad · Jul 4, 2013

Try this:

// read input file
$fp = fopen( 'test_tab.txt', 'r' );

// write to an output file
$fp1 = fopen( 'output.txt', 'w' );

while( ( $data = fgetcsv( $fp, '', "\t" ) ) !== FALSE ) {
    $list = explode( ';', $data[ 2 ] );

    foreach( $list as $column ) {
        fwrite( $fp1, '"' . $data[ 0 ] . "\"\t\"" . $data[ 1 ] . "\"\t\"" . trim( $column ) . "\"\n" );
    }
}

fclose( $fp1 );
fclose( $fp );

Hope this helps.