How to Read a CSV File & Convert It into an Array in Magento 2
The following article describes how to read a CSV file and convert it into an array in Magento 2. We also shed light on an alternative way of transferring data to your e-commerce website, when you don’t need to perform complicated manipulations over CSV files or can eliminate them from data transfer entirely. More useful tips are available in our Magento 2 Cookbook.
How to read a CSV file and convert it into an array in Magento 2
As usually, we seek for help on the StackExchange forum. The platform offers tons of solutions to all possible issues posted by community members. Thus, you can rely on the support of enthusiasts and specialists all over the world. And the problem of reading CSV files and converting them into an array is not an exception.
1 2 3 4 5 6 7 8 9 10 |
<div class="import-shipping"> <div class="header"> <h4>CSV File:</h4> </div> <form class="import-shipping-form" action="<?= $postUrl ?>" method="POST" enctype="multipart/form-data"> <?php echo $block->getBlockHtml('formkey'); ?> <input type="file" required class="required-entry" name="csv" /> <button type="submit">Import</button> </form> </div> |
The main goal is to read the CSV file and turn it into an array. Besides, it is necessary to validate the file input type. The data looks like this:
According to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
protected $csv; public function __construct( \Magento\Framework\File\Csv $csv ) { $this->csv = $csv; } //The function name should match your controller path public function import($file) { if (!isset($file['tmp_name'])) throw new \Magento\Framework\Exception\LocalizedException(__('Invalid file upload attempt.')); $csvData = $this->csv->getData($file['tmp_name']); foreach ($csvData as $row => $data) { if ($row > 0){ //Start your work } } die(); } |
Also, note that $data is already an array object. Thus, it looks like
1 2 3 4 5 |
array( 0 => 'UPS', 1 => 'One Night Service' ...... ) |
The author of the answer also added if ($row > 0) to skip the first row. The reason to do that is that it’s the attribute row only.
It is also worth mentioning that you should die() the execution after the job is done. This solution works for a custom made controller. To override the core controller, read another material: How to Override Core Block, Model & Controller in Magento 2.
The author also provides the following reference:
How to transfer any CSV file to Magento 2
You can avoid using standard import/export tools to transfer any CSV file into Magento 2. Furthermore, there is a solution that prevents you from creating your custom plugins. Meet your number one assistant for data transfers – the Improved Import & Export Magento 2 extension. With the help of this module, you will no longer need to perform complex operations related to reading and converting CSV files.
Our extension lets you transfer data using different file standards. You can even automate import and export processes. And when a data file contains unsupported attributes or values, it is no longer a problem.
Improved Import & Export offers the following benefits over the native import tools of Magento 2:
- All import issues related to the default tool are fixed.
- The extension can import even large files very vast by splitting the data into groups.
- Improved Import & Export covers not only products and customers but also orders, categories, B2B, MSI, and other entities.
- With support for multiple file types, you no longer need to convert other files to|from CSV.
- Different file sources let you import data files from Dropbox, FTP, and SFTP.
- Alternative data transfers include Google Sheets and API so that you can avoid file transfers.
- With attribute mapping, you can make any third-party data files compatible with Magento 2.
- And our module lets you export only relevant data with the help of filters.
- The Improved Import & Export plugin has a user-friendly admin interface but also provides the ability to use dedicated CLI commands.
- The extension gets several updates per year with 20+ changelog items.
- Due to the explicit documentation, you can find everything you need to know about the Improved Import & Export extension.
While comparing the Improved Import & Export Magento 2 extension with competitors, you will see the following advantages of our module:
- One-time purchase: no SAAS, recurring payments, or IPAAS models.
- Create a custom Magento 2 import solution based on our module since it has an open code.
- Control all data flows since the extension is installed on your Magento 2.
- The module covers numerous use-cases from migration to warehouse management.
- During the support period, we consult, assist, and do small customizations.
- The Improved Import & Export plugin works with all basic Magento 2 entities.
- You can rely on APIs to connect your e-commerce website with any CRM, ERP, PIM, and other external systems.
- The module is built with native and third-party software in mind to avoid conflicts.
- Our top Magento 2 specialists use best coding practices to stay on the verge of technology.
Also, note that along with the Improved Import & Export extension, we offer the following services and customizations:
- Migration from Magento 1 to Magento 2.
- Migration from external systems (Shopify, Bigcommerce, Prestashop, and Wocommerce) to Magento 2.
- Setup and customization of multi-channel sales and dropshipping models based on our module.
- Two-way connection with CRM, ERP, and PIM systems via REST/SOAP APIs.
- Assitance with custom Magento 2 feed export.
- Creation of XSLT templates for XML files.
- Other customization services
Contact Us to describe your particular business case, and we will provide the most efficient solution on top of Improved Import & Export. You don’t need to know how to read a CSV file and convert it into an array to transfer data to your Magento 2 store. Our specialists will do this for you.
Final Words
The Improved Import & Export extension is a robust solution that eliminates various routine processes providing the most efficient way to transfer data to your e-commerce website. If you have any issues with reading a CSV file and converting it into an array in Magento 2, follow the link below and contact our support:
Get Improved Import & Export Magento 2 Extension