Skip to content

Instantly share code, notes, and snippets.

@real34
Created February 19, 2014 14:49
Show Gist options
  • Save real34/9093589 to your computer and use it in GitHub Desktop.
Save real34/9093589 to your computer and use it in GitHub Desktop.
Filter CSV files in a quick'n'dirty way
<?php
$dir = __DIR__;
function isValid($product) {
return !empty($product[2]);
}
if (($productsFile = fopen($dir . '/products2.csv', "r")) !== FALSE) {
$missingSkus = array_map('trim', file($dir . '/missing-images-sku.csv'));
$outputOfMissingImages = fopen($dir . '/out/products-without-images.csv', 'w+');
$errorsFile = fopen($dir . '/out/errors.csv', 'w+');
// headers
$fields = fgetcsv($productsFile, 1000, ",");
$fieldsToKeep = array('sku', 'image', 'small_image', 'media_gallery', 'thumbnail');
fputcsv($outputOfMissingImages, $fieldsToKeep);
while (($product = fgetcsv($productsFile, 1000, ",")) !== FALSE) {
if (count($product) == count($fields)) {
$indexedProduct = array_combine($fields, $product);
if (in_array($indexedProduct['sku'], $missingSkus)) {
fputcsv($outputOfMissingImages, array_intersect_key($indexedProduct, array_flip($fieldsToKeep)));
}
} else {
fputcsv($errorsFile, $product);
}
}
fclose($productsFile);
fclose($errorsFile);
fclose($outputOfMissingImages);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment