Read CSV data magento category import

This tutorial explains how to read CSV data for magento category import programmatically. The following php code read the CSV data from the sample file.

Please also refer the tutorials that explains clearly how to import magento 2 and magento version 1 categories programmatically. The script create the categories as shown below.

Import magento 2 categories programmatically

function getCategories()
{
    $file = 'csv/categories.csv';
    $arrResult = array();
    $headers = false;
    $handle = fopen($file, "r");
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers) {
                $headers[] = $data;
            } else {
                $arrResult[] = $data;
            }
        }
        fclose($handle);
    }
    return $arrResult;
}

Share this Tutorial