Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active March 31, 2016 14:38
Show Gist options
  • Save kurozumi/1fafc828adf85b1149adc99653555c6f to your computer and use it in GitHub Desktop.
Save kurozumi/1fafc828adf85b1149adc99653555c6f to your computer and use it in GitHub Desktop.
【PHP】League\Csvを使ってCSVファイルの任意のカラムの重複したデータを削除する方法
<?php
require_once 'vendor/autoload.php';
use League\Csv\Reader;
$str = <<<EOF
john,doe,1
jane,doe,2
foo,bar,3
EOF;
$reader = Reader::createFromString($str);
/**
* 重複データ削除
*
* @param $iterator
* @param int $offsetIndex 重複データを削除したい列を指定
* @return Generator
*/
function deduplicate($iterator, $offsetIndex=0)
{
foreach ($iterator as $row) {
yield $row[$offsetIndex] => implode(",", $row);
}
}
$result = iterator_to_array(deduplicate($reader, 1));
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment