Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 788
ictl Avatar asked Jul 04 '13 09:07

ictl


2 Answers

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.

like image 55
web-nomad Avatar answered Oct 20 '22 17:10

web-nomad


Your text must be seperated with tab and then try this one,

<?php $mainarray = array(); 
foreach (file('test_tab.txt') as $row) { 
  $ar = explode("\t", $row); 
  $mainarray[] = array('header1' => $ar[0], 'header2' => $ar[1], 'header3' => $ar[2]); 
} 

var_dump($mainarray);?>
like image 25
Sherin Jose Avatar answered Oct 20 '22 17:10

Sherin Jose