title: How to combine rows with the same id from a csv with php
body: <p>I have a csv with two columns "id" and "name" and I want to merge the rows that share the same id and to have them be separated by a comma.</p>
  
  <pre><code>id      name
  39695   NFL: Cowboys @ Giants
  39699   NFL: Colts @ Bears
  39699   NFL: Steelers @ Broncos
  39699   NFL: Eagles @ Browns
  39699   NFL: Panthers @ Buccaneers
  </code></pre>
  
  <p>into</p>
  
  <pre><code>id      name
  39695   NFL: Cowboys @ Giants
  39699   NFL: Colts @ Bears, NFL: Steelers @ Broncos, NFL: Eagles @ Browns, NFL: Panthers @ Buccaneers
  </code></pre>
  
  <p>All I need is just an echo back out or a new .csv file. </p>
  
  <p>Thank you for the help.</p>
  
  <p>**edit</p>
  
  <pre><code>&lt;"?php
  
  $lines = file('sport3.csv');
  
  foreach($lines as $line)
  {
  list($id, $name) = explode(',', $line);
  $merged[$id][] = $name;
  }
  
  foreach($merged as $id =&gt; $vals)
  {
  echo '"' . $id . '", "' . implode(',', $vals) . '\"\n';
  }
  ?&gt;
  </code></pre>