Skip to content

Instantly share code, notes, and snippets.

@rana01645
Created July 22, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rana01645/1431bb81f54a0d8ebedd4dde26145ddf to your computer and use it in GitHub Desktop.
Save rana01645/1431bb81f54a0d8ebedd4dde26145ddf to your computer and use it in GitHub Desktop.
vel CSV spceific column modifier based on another file
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