删除 CSV 文件特定列中具有重复值的行

我这里有 data.csv:


id: 10, location: Canada, people: 12

id: 10, location: United States, people: 15

id: 15, location: England, people: 19

id: 16, location: India, people: 20

id: 16, location: Germany, people: 9

我希望它使用 PHP 输出:


id: 10, location: Canada, people: 12

id: 15, location: England, people: 19

id: 16, location: India, people: 20

通过删除第一列中具有相同值的行。我怎样才能做到这一点?(我是 PHP 新手,真的不知道在这里要做什么;我尝试了其他人为类似问题制作的一些脚本,但它们似乎不起作用)我更希望它回显结果而不是覆盖或创建一个新文件。


MM们
浏览 296回答 2
2回答

杨魅力

使用 fgetcsv 逐行读取 csv 并创建数组,其中“:”后面的内容是键,后面的内容是值。然后您可以删除重复项。当您只有数据时,您需要构建 csv 字符串。您可以直接使用它或将其存储在输出 csv 文件中。<?php$handle = fopen("data.csv", "r");// parse csv line by line and create data array with its information$data = [];while (($row = fgetcsv($handle)) !== false) {&nbsp; $newRow = [];&nbsp; foreach ($row as $field) {&nbsp; &nbsp; &nbsp;$parts = explode(':', $field);&nbsp; &nbsp; &nbsp;$key = trim($parts[0]);&nbsp; &nbsp; &nbsp;$value = trim($parts[1]);&nbsp; &nbsp; &nbsp;$newRow[$key] = $value;&nbsp; }&nbsp; $data[] = $newRow;}// iterate data and remove duplicate ids - keep only first id occurence$indexedData = [];foreach ($data as $row) {&nbsp; if (!isset($indexedData[$row['id']])) {&nbsp; &nbsp; $indexedData[$row['id']] = $row;&nbsp; }}var_dump($indexedData);// create csv string with new data$result = '';foreach ($indexedData as $row) {&nbsp; $fields = [];&nbsp; foreach ($row as $key => $value) {&nbsp; &nbsp; $fields[] = $key.': '.$value;&nbsp; }&nbsp; $result .= implode(', ', $fields).PHP_EOL;}var_dump($result);$索引数据:array(3) {&nbsp; [10]=>&nbsp; array(3) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(2) "10"&nbsp; &nbsp; ["location"]=>&nbsp; &nbsp; string(6) "Canada"&nbsp; &nbsp; ["people"]=>&nbsp; &nbsp; string(2) "12"&nbsp; }&nbsp; [15]=>&nbsp; array(3) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(2) "15"&nbsp; &nbsp; ["location"]=>&nbsp; &nbsp; string(7) "England"&nbsp; &nbsp; ["people"]=>&nbsp; &nbsp; string(2) "19"&nbsp; }&nbsp; [16]=>&nbsp; array(3) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; string(2) "16"&nbsp; &nbsp; ["location"]=>&nbsp; &nbsp; string(5) "India"&nbsp; &nbsp; ["people"]=>&nbsp; &nbsp; string(2) "20"&nbsp; }}$结果:string(111) "id: 10, location: Canada, people: 12id: 15, location: England, people: 19id: 16, location: India, people: 20"或者,如果您不关心 csv 中的数据(例如您不需要人数统计等),这里是更简单的版本:<?php$handle = fopen("data.csv", "r");$data = [];while (($row = fgetcsv($handle)) !== false) {&nbsp; if (!isset($data[$row[0]])) {&nbsp; &nbsp; $data[$row[0]] = $row;&nbsp; }}$result = '';foreach ($data as $row) {&nbsp; $result .= implode(',', $row).PHP_EOL;}var_dump($result);$结果是一样的。

守候你守候我

您实际上不需要解析整行数据。一次preg_replace()调用即可删除后来出现的重复行。以下模式仅用于处理彼此相邻的重复行。它不是为了处理由非重复项分隔的重复项而构建的。代码:(演示)echo preg_replace(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'/(^id: (\d+),.+)(?:\Rid: \2,.+)+/m',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'$1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;file_get_contents('data.csv')&nbsp; &nbsp; &nbsp;);或者,您可以使用单个循环并维护一个查找数组来确定之前是否已回显 id。即使重复行被非重复行分隔开,这也将起作用。代码:(演示)foreach (explode(PHP_EOL, $csv) as $line) {&nbsp; &nbsp; $firstColumn = strtok($line, ',');&nbsp; &nbsp; if (!isset($lookup[$firstColumn])) {&nbsp; &nbsp; &nbsp; &nbsp; echo $line . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; $lookup[$firstColumn] = true;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP