shakey
shakey

Reputation: 11

How to merge lines of CSV in PHP

I have a CSV file that looks something like:

Andrew Smith, [email protected], Manchester, UK
Martin Jones, [email protected], Reading, UK
Shirley Andrews, , Los Angeles,
Bob James, [email protected], ,

Jacob Costelloe, [email protected], Sydney, Australia
Shirley Andrews, [email protected], , US
Callum Jones, [email protected], Paris, France
Bob James, , London, UK

Based on the first column (name), I need to merge the data, so returned from this I would get:

Andrew Smith, [email protected], Manchester, UK
Martin Jones, [email protected], Reading, UK
Bob James, [email protected], London, UK
Jacob Costelloe, [email protected], Sydney, Australia
Shirley Andrews, [email protected], Los Angeles, US
Callum Jones, [email protected], Paris, France

Is there a simple way to do this in PHP?

Upvotes: 1

Views: 2631

Answers (2)

nickb
nickb

Reputation: 59709

How about something like this: http://codepad.org/MfYAycZk

As you can see, the output correlates the two Bob James together.

$text = "Andrew Smith, [email protected], Manchester, UK
Martin Jones, [email protected], Reading, UK
Shirley Andrews, , Los Angeles,
Bob James, [email protected], ,
Jacob Costelloe, [email protected], Sydney, Australia
Shirley Andrews, [email protected], , US
Callum Jones, [email protected], Paris, France
Bob James, , London, UK";

$people = array();
foreach( explode( "\n", $text) as $line)
{
    $entries = explode( ',', $line);
    $entries = array_map( 'trim', $entries);
    list( $first_name, $last_name) = explode( ' ', $entries[0]);

    if( isset( $people[ $last_name ][ $first_name ])) {
        $people[ $last_name ][ $first_name ] = array_merge( $people[ $last_name ][ $first_name ], array( 
            'first_name' => $first_name,
            'last_name' => $last_name,
            'email' => $entries[1],
            'city' => $entries[2],
            'country' => $entries[3]

        ));

    } else {

        $people[ $last_name ][ $first_name ] = array( 
            'first_name' => $first_name,
            'last_name' => $last_name,
            'email' => $entries[1],
            'city' => $entries[2],
            'country' => $entries[3]
        );
    }
}

var_dump( $people);

Upvotes: 1

Jason Brumwell
Jason Brumwell

Reputation: 3550

This will format the text how you described:

$text = str_replace("\r", '', $text);

$container = explode("\n", $text);
$rows = array();

foreach ($container as $data) {
    $values = array_map('trim', explode(',', $data));

    $id = strtolower(trim($values[0]));

    if ($id) {                                
        if (true == isset($rows[$id])) {
            //remove blank
            $values = array_filter($values);

            foreach ($values as $i => $value) {
                if ($rows[ $id ][ $i ] == '') {
                   $rows[ $id ][ $i ] = $value;
                }
            }
       } else {
            $rows[ $id ] = $values;
       }
    }
}

Upvotes: 0

Related Questions