Last active
March 31, 2016 14:38
-
-
Save kurozumi/1fafc828adf85b1149adc99653555c6f to your computer and use it in GitHub Desktop.
【PHP】League\Csvを使ってCSVファイルの任意のカラムの重複したデータを削除する方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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