Created
February 19, 2014 14:49
-
-
Save real34/9093589 to your computer and use it in GitHub Desktop.
Filter CSV files in a quick'n'dirty way
This file contains hidden or 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 | |
$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