Created
July 22, 2020 19:52
-
-
Save rana01645/1431bb81f54a0d8ebedd4dde26145ddf to your computer and use it in GitHub Desktop.
vel CSV spceific column modifier based on another file
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
use Illuminate\Support\Collection; | |
function csvToArray($filename = '', $delimiter = ',') | |
{ | |
if (!file_exists($filename) || !is_readable($filename)) | |
return false; | |
$header = null; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== false) | |
{ | |
while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) | |
{ | |
if (!$header) | |
$header = $row; | |
else | |
$data[] = array_combine($header, $row); | |
} | |
fclose($handle); | |
} | |
return $data; | |
} | |
$csvFileName = "err_accounts.csv"; | |
$csvFile = '/project_name/public/storage/csv/all_accounts.csv'; | |
$all_accounts = new Collection(csvToArray($csvFile)); | |
// dump($all_accounts->count()); | |
$csvFile = '/project_name/public/storage/csv/err_accounts.csv'; | |
$err_accounts = new Collection(csvToArray($csvFile)); | |
$updated_acc = $err_accounts->map(function($acc) use($all_accounts){ | |
$account = $all_accounts->where('Email',$acc['Username'])->first(); | |
if($account){ | |
$acc['Password'] = $account['Password']; | |
} | |
return $acc; | |
}); | |
$csvFile = '/project_name/public/storage/csv/err_accounts_fixed.csv'; | |
$fp = fopen($csvFile, 'wb'); | |
foreach ($updated_acc as $fields) { | |
fputcsv($fp, $fields); | |
} | |
fclose($fp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment