How to retrieve CSV data as array pairs in Magento 2?

Generally, CSV file contains comma separated value for import the data into Magento admin.

You can Retrieve CSV file data as pairs by just calling Magento\Framework\File\Csv class. Csv.php file contains getDataPairs() function to convert the first column as key and second column as the value in the array from the CSV file.

Let’s assume your CSV file contains below value for the first column and a second column with comma separated,

We need to convert it into array pair using Magento 2.

In the Block file, I have passed the CSV file path as $file variable. first, verify CSV exists or not, if file exists, convert it to an array and gives output.

Call from template file,

echo $block->getFileData();

Output: