Home > Enterprise >  How to fetch data from a CSV file in PHP with different number of rows and columns?
How to fetch data from a CSV file in PHP with different number of rows and columns?

Time:09-21

I am creating a WordPress custom plugin to import product data in Woocommerce by uploading a CSV file. The CSV file contains different kinds of products (simple, variable, grouped, external) with custom attributes and a lot of data. You can look into the enter image description here

MY GOAL : I want to upload a CSV file containing data of mixed product types like 100 simple products, 50 variable products, 20 external products, and 10 grouped products and then upload all products in Woocommerce.

CodePudding user response:

ParseCSV for PHP worked for me. If someone gets into the same situation, they can use this PHP class: https://github.com/parsecsv/parsecsv-for-php It returns CSV file data properly.

After opening the CSV file in PHP and getting file name, I add this PHP from code:

<?PHP 

    $csv = new \ParseCsv\Csv();
    $csv->auto($csvFileName);
    $csv_file_data = $csv->data;

?>

Then, I only needed to iterate the $csv_file_data variable using foreach loop.

Specially thanks for @Deigo referring to that PHP class.

  • Related