Read magento attribute options CSV file

The tutorial explains how to read magento attribute options CSV file. Here it shows how to read data from the options.csv file programmatically and function create an array.

Please refer the tutorials that explains how to create attribute options in magento 1 and magento 2 programmatically as show below.

create attribute options magento 2 programmatically

Read magento attribute options CSV file

function readOptions() {
    
    $options = $uniqueValues = array();
    $file = 'csv/options.csv';
    $handle = fopen($file, "r");
    $headers = false;
    
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers) {
                $headers[] = $data;
            } else {
                 $options[] = trim($data[4]);
            }
        }
        fclose($handle);
    }
    $uniqueValues = array_unique($options);
    asort($uniqueValues);
    $optionValues = array_values($uniqueValues);
    createAttribute($optionValues);
}

Share this Tutorial