我有一个包含两列“id”和“name”的 csv,我想合并共享相同 id 的行并用逗号分隔它们。
id name
39695 NFL: Cowboys @ Giants
39699 NFL: Colts @ Bears
39699 NFL: Steelers @ Broncos
39699 NFL: Eagles @ Browns
39699 NFL: Panthers @ Buccaneers
进入
id name
39695 NFL: Cowboys @ Giants
39699 NFL: Colts @ Bears, NFL: Steelers @ Broncos, NFL: Eagles @ Browns, NFL: Panthers @ Buccaneers
我所需要的只是一个回显或一个新的 .csv 文件。
感谢您的帮助。
**编辑
<"?php
$lines = file('sport3.csv');
foreach($lines as $line)
{
list($id, $name) = explode(',', $line);
$merged[$id][] = $name;
}
foreach($merged as $id => $vals)
{
echo '"' . $id . '", "' . implode(',', $vals) . '\"\n';
}
?>