Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【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