3

I have a tab delimited text file like this:

"abcdef1"   "AB"    
"abcdef1"   "CD"    
"ghijkl3"   "AA"    
"ghijkl3"   "BB"    
"ghijkl3"   "CC"    

For every common ID (e.g. abcdef1), I need to take the two digit code an concatenate it into a multi-value. So, eventually it should look like:

"abcdef1" "AB,CD"

"ghijk13", "AA,BB,CC"

I dont need to create a new output txt file but if i can get the final values in an array that would be great. I am just a week old to php, hence looking for help with this. I was able to get the values from the input txt file into an array, but further processing the array to get the common ID and take the 2 digit code and concatenate is something I'm struggling with. Any help is greatly appreciated

0

2 Answers 2

4

How about:

$values = array();
$handle = fopen($file, 'r');

// get the line as an array of fields
while (($row = fgetcsv($handle, 1000, "\t")) !== false) {
    // we haven't seen this ID yet
    if (!isset($values[$row[0]])) {
        $values[$row[0]] = array();
    }

    // add the code to the ID's list of codes
    $values[$row[0]][] = $row[1];
}

$values will be something like:

Array
(
    [abcdef1] => Array
        (
            [0] => AB    
            [1] => CD    
        )

    [ghijkl3] => Array
        (
            [0] => AA    
            [1] => BB    
            [2] => CC  
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

There are a number of steps to the task you want to do. The first step, obviously, is getting the contents of your file. You state that you've already been able to get the contents of the file into an array. You may have done something like this:

// Assuming that $pathToFile has the correct path to your data file

$entireFile = file_get_contents( $pathToFile );
$lines = explode( '\n', $entireFile ); // Replace '\n' with '\r\n' if on Windows

How you get the lines into the array is less important. From here on out I assume that you've managed to fill the $lines array. Once you have this, the rest is fairly simple:

// Create an empty array to store the results in
$results = array();

foreach( $lines as $line ){

    // Split the line apart at the tab character
    $elements = explode( "\t", $line );

    // Check to see if this ID has been seen
    if( array_key_exists( $elements[0], $results ){

        // If so, append this code to the existing codes for this ID (along with a comma)
        $results[ $elements[0] ] .= ',' . $elements[1];

    } else {

        // If not, this is the first time we've seen this ID, start collecting codes
        $results[ $elements[0] ] = $elements[1];
    }
}

// Now $results has the array you are hoping for

There are some variations on this -- for example, if you want to get rid of the quote marks around each ID or around each code, you can replace $results[ $elements[0] ] with $results[ trim( $elements[0], '"' ) ] and/or replace $elements[1] with trim( $elements[1], '"' ).

1 Comment

Thanks a bunch @cbuckley and @Jazz! I had the initial array built pretty much as Jazz mentioned, but I somehow missed array_key_exists() and was trying to implement that using a temp array and was going nowhere with it. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.