Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:02:53+00:00 2026-05-31T00:02:53+00:00

I have a csv-files like this, names.csv: id,name 1,John 2,Joe 3,Peter 4,Frank 5,Bill …

  • 0

I have a csv-files like this, names.csv:

"id","name"
"1","John"
"2","Joe"
"3","Peter"
"4","Frank"
"5","Bill"
...
"2","Steve"
"5","James"
"2","Mark"
"4","Anthony"

I want to merge the “rows” with the same “id” so I reach the following result:

"id","name"
"1","John"
"2","Joe , Steve , Mark"
"3","Peter"
"4","Frank , Anthony"
"5","Bill , James"

I want to do this with php language, but I don’t get a solution.
By my self I made some code, but that makes to much work for my serverhost (loops in loops):

$myFile = "merged-names.csv";
$fh = fopen($myFile, 'w') or die("can't open file");

if (($handle = fopen("names.csv", "r")) !== FALSE) 
{
  $line = fgets($handle);
  $val = explode(",", $line);
  $max = (count($val))/2;

  for ($i=0; $i <= $max; $i+2)
  {
    $id = $val[$i];
    $name = $val[$i+1];
    $stringData = "\"".$id."\"".",";
    fwrite($fh, $stringData);
    $stringData = "\"".$name."\"";
    fwrite($fh, $stringData);

    for ($e=0; $e <= $max; $e+2)
    {
  if (strcmp($id, $val[$e]) == 0)
  {
        if($i != $e)
    {
      $stringData = "\"".$val[$e+1]."\"";
      fwrite($fh, $stringData);
        }
  }
    }

    $stringData = "<br>";
    fwrite($fh, $stringData);

  }
fclose($handle);
 }

Can someone help me with the right solution for this, seems so simply, problem?
Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:02:54+00:00Added an answer on May 31, 2026 at 12:02 am

    how about this…

    $temp = array();
    if (($handle = fopen("names.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $name = $data[1];
            $id = $data[0];
            if (!isset($temp[$id]))
                $temp[$id] = array();
            $temp[$id][$name] = 1;
        }
        fclose($handle);
    }
    foreach ($temp as $k => $v) {
        echo $k.","."\"".implode(",", array_keys($v))."\""."\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file formatted just like this: name,color,tasty,qty apple,red,true,3 orange,orange,false,4 pear,greenish-yellowish,true,1 As
I have a large number of csv files that look like this below: xxxxxxxx
I have a Excel CSV files with employee records in them. Something like this:
Ok, I have a csv file like this: 14 ; 1234,56 ; 10203 ;
I have a CSV file, formatted like this: 0001 @ word @ some information
Hey all. So I have a collection of csv files which I would like
I have 500 CSV files in this format: IndicatorA_Name.csv 1900 1901 1902 ... Norway
I have one temp folder C:\Mydata inside that i have .csv files with name
I have a file called test.csv, Data in the file is looking like this:
I have a file that looks like this ID Name Car 1 Mike Honda

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.