$subProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sub_pr_sku);
 }
 $range_arr = array('10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40-44', '45-49', '50-54', '55-59', '60-64', '65-69', '70-74');
 $et_range = '';
 for ($n = 0; $n < count($range_arr); $n++) {
     $exp_arr = explode('-', $range_arr[$n]);
     if ($exp_arr[0] <= trim($data['AREPTI']) && $exp_arr[1] >= trim($data['AREPTI'])) {
         //echo $exp_arr[0]."<=".trim($data['AREPTI'])."----".$exp_arr[1].">=".trim($data['AREPTI']);
         // echo "====>>>>";
         $et_range = $range_arr[$n];
         //echo "Range=".$range_arr[$n];
         break;
     }
 }
 if ($sub_product_id == '') {
     $subProductData = array('name' => $pr_title, 'sku' => $sub_pr_sku, 'weight' => trim($data['WEIGHT']), 'status' => 1, 'visibility' => '4', 'attribute_set_id' => 4, 'type_id' => 'simple', 'price' => trim($data['ARVK4']), 'store_id' => $storeID, 'category_ids' => $cats, 'website_ids' => $websiteIDs, 'tax_class_id' => 0, 'page_layout' => 'one_column', 'short_size' => $short_size, 'center_hole' => trim($data['ARNABO']), 'brand' => addAttributeValue('brand', ucfirst(strtolower(trim($brand_str)))), 'et_measurement' => addAttributeValue('et_measurement', trim($data['AREPTI'])), 'article_code' => trim($data['ARHANR']), 'rim_width' => addAttributeValue('rim_width', trim(str_replace(",", ".", trim($data['ARSBR'])))), 'et_range' => attributeValueExists('et_range', $et_range), 'diameter' => addAttributeValue('diameter', trim($data['ARSZG'])), 'bolt_circle' => addAttributeValue('bolt_circle', trim($bolt_circle)), 'product_type_code' => trim($data['ARRART']), 'article_group1' => trim($data['ARAGR']), 'article_group2' => trim($data['ARANR']), 'ean_number' => trim($data['EAN']), 'description' => "Content for " . $pr_title, 'short_description' => "Short Content for " . $pr_title);
     foreach ($subProductData as $key => $value) {
         $subProduct->setData($key, $value);
     }
     $subProduct->setStockData(array('manage_stock' => 1, 'is_in_stock' => 1, 'qty' => trim($data['ARVLB']), 'use_config_manage_stock' => 0));
 } else {
     $subProduct->setData('price', trim($data['ARVK4']));
     $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($sub_product_id);
     $stockItemId = $stockItem->getId();
     $stockItem->setData('manage_stock', 1);
     $stockItem->setData('qty', (int) $data['ARVLB']);
     $stockItem->save();
 }
 $subProduct->save();
 if ($sub_product_id == '') {
     $sub_product_id = $subProduct->getId();
 for ($n = 0; $n < count($range_arr); $n++) {
     $exp_arr = explode('-', $range_arr[$n]);
     if ($exp_arr[0] <= trim($data['Offset']) && $exp_arr[1] >= trim($data['Offset'])) {
         //echo $exp_arr[0]."<=".trim($data['AREPTI'])."----".$exp_arr[1].">=".trim($data['AREPTI']);
         // echo "====>>>>";
         $et_range = $range_arr[$n];
         //echo "Range=".$range_arr[$n];
         break;
     }
 }
 $price = '';
 $price = trim($data['SalesPrice']);
 $final_prc = $price * ($discount_amount / 100);
 $final_prc = $price - $final_prc;
 if ($sub_product_id == '') {
     $subProductData = array('name' => $pr_title, 'sku' => $sub_pr_sku, 'weight' => 1, 'status' => 1, 'visibility' => '4', 'attribute_set_id' => 4, 'type_id' => 'simple', 'price' => $final_prc, 'msrp' => $price, 'store_id' => $storeID, 'category_ids' => $cats, 'website_ids' => $websiteIDs, 'tax_class_id' => 2, 'page_layout' => 'one_column', 'short_size' => $short_size, 'center_hole' => trim(round((double) $data['CenterBore'], 1)), 'brand' => addAttributeValue('brand', ucfirst(strtolower(trim($brand_str)))), 'et_measurement' => addAttributeValue('et_measurement', trim($data['Offset'])), 'article_code' => trim($data['Article']), 'rim_width' => addAttributeValue('rim_width', trim(str_replace(",", ".", $width))), 'et_range' => attributeValueExists('et_range', $et_range), 'diameter' => addAttributeValue('diameter', $diameter), 'bolt_circle' => addAttributeValue('bolt_circle', trim($bolt_circle)), 'description' => $data['Description'], 'short_description' => $data['Description'], 'rim_product_group' => addAttributeValue('rim_product_group', $season));
     foreach ($subProductData as $key => $value) {
         $subProduct->setData($key, $value);
     }
     $subProduct->setStockData(array('manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 1000, 'use_config_manage_stock' => 0));
 } else {
     $subProduct->setData('description', $data['Description']);
     $subProduct->setData('short_description', $data['Description']);
     $subProduct->setData('price', $final_prc);
     $subProduct->setData('msrp', $price);
     $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($sub_product_id);
     $stockItemId = $stockItem->getId();
     $stockItem->setData('manage_stock', 1);
     $stockItem->setData('qty', 1000);
     $stockItem->save();
 }
 if ($parent_product_id == '') {
     $parent_product_id = $sProduct->getId();
 }
 /* Add or update child product */
 $subProductData = array();
 $pr_title = $data['Breite'] . "/" . $data['ARSQU'] . " R" . $data['ARSZG'] . " " . $data['Tragfaehigkeit'] . $data['ARSGI'] . " " . $tyre_type_arr[$data['ARRART']];
 $sub_pr_sku = trim($data['ARAGR']) . trim($data['ARANR']) . "_Tyre";
 $short_size = $data['Bezeichnung'];
 $sub_product_id = Mage::getModel("catalog/product")->getIdBySku($sub_pr_sku);
 if ($sub_product_id == '') {
     $subProduct = Mage::getModel('catalog/product');
 } else {
     $subProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sub_pr_sku);
 }
 if ($sub_product_id == '') {
     $subProductData = array('name' => trim($pr_title), 'sku' => $sub_pr_sku, 'weight' => trim($data['ARGEW']), 'status' => 1, 'visibility' => '4', 'attribute_set_id' => 9, 'type_id' => 'simple', 'price' => trim($data['VK8']), 'store_id' => $storeID, 'category_ids' => $cats, 'website_ids' => $websiteIDs, 'tax_class_id' => 2, 'page_layout' => 'one_column', 'short_size' => trim($data['Bezeichnung']), 'load_capacity' => addAttributeValue('load_capacity', trim($data['Tragfaehigkeit'])), 'speed_index' => addAttributeValue('speed_index', trim($data['ARSGI'])), 'ean_number' => trim($data['AREAN']), 'article_code' => trim($data['ARHANR']), 'noise' => trim($data['LaermDB']), 'brand' => addAttributeValue('brand', ucfirst(strtolower(trim($hersteller_str)))), 'diameter' => addAttributeValue('diameter', trim($data['ARSZG'])), 'width' => addAttributeValue('width', trim($data['Breite'])), 'profile' => addAttributeValue('profile', trim($data['ARSQU'])), 'fuel_efficiency' => addAttributeValue('fuel_efficiency', trim($data['Nasshaftung'])), 'wet_grip' => addAttributeValue('wet_grip', trim($data['Rollwiderstand'])), 'product_type_code' => trim($data['ARRART']), 'article_group1' => trim($data['ARAGR']), 'article_group2' => trim($data['ARANR']), 'article_number' => trim($data['ARAGR']) . trim($data['ARANR']), 'article_code_new' => trim($data['AREAN']), 'model_extension' => trim($data['Zusatzbezeichnung']), 'product_group' => addAttributeValue('product_group', $season), 'tyre_type' => $tyre_type_val, 'description' => "Content for " . $pr_title, 'short_description' => "Short Content for " . $pr_title);
     foreach ($subProductData as $key => $value) {
         $subProduct->setData($key, $value);
     }
     $subProduct->setStockData(array('manage_stock' => 1, 'is_in_stock' => 1, 'qty' => trim($data['verfLB']), 'use_config_manage_stock' => 0));
 } else {
     $subProduct->setData('price', trim($data['VK8']));
     $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($sub_product_id);
     $stockItemId = $stockItem->getId();
     $stockItem->setData('manage_stock', 1);
     $stockItem->setData('qty', (int) $data['verfLB']);
     $stockItem->setData('is_in_stock', 1);
     $stockItem->save();
 }
 $subProduct->setData('is_deleted_prpduct', '1');
 $subProduct->save();
/**
 * Create an attribute.
 *
 * For reference, see Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
 *
 * @return int|false
 */
function createAttribute($labelText, $attributeCode, $values = -1, $productTypes = -1, $setInfo = -1, $options = -1)
{
    $labelText = trim($labelText);
    $attributeCode = trim($attributeCode);
    if ($labelText == '' || $attributeCode == '') {
        echo "Can't import the attribute with an empty label or code.  LABEL= [{$labelText}]  CODE= [{$attributeCode}]" . "<br/>";
        return false;
    }
    if ($values === -1) {
        $values = array();
    }
    if ($productTypes === -1) {
        $productTypes = array();
    }
    if ($setInfo !== -1 && (isset($setInfo['SetID']) == false || isset($setInfo['GroupID']) == false)) {
        echo "Please provide both the set-ID and the group-ID of the attribute-set if you'd like to subscribe to one." . "<br/>";
        return false;
    }
    echo "Creating attribute [{$labelText}] with code [{$attributeCode}]." . "<br/>";
    //>>>> Build the data structure that will define the attribute. See
    //     Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
    $data = array('is_global' => '0', 'frontend_input' => 'text', 'default_value_text' => '', 'default_value_yesno' => '0', 'default_value_date' => '', 'default_value_textarea' => '', 'is_unique' => '0', 'is_required' => '0', 'frontend_class' => '', 'is_searchable' => '1', 'is_visible_in_advanced_search' => '1', 'is_comparable' => '1', 'is_used_for_promo_rules' => '0', 'is_html_allowed_on_front' => '1', 'is_visible_on_front' => '0', 'used_in_product_listing' => '0', 'used_for_sort_by' => '0', 'is_configurable' => '0', 'is_filterable' => '0', 'is_filterable_in_search' => '0', 'backend_type' => 'varchar', 'default_value' => '', 'is_user_defined' => '0', 'is_visible' => '1', 'is_used_for_price_rules' => '0', 'position' => '0', 'is_wysiwyg_enabled' => '0', 'backend_model' => '', 'attribute_model' => '', 'backend_table' => '', 'frontend_model' => '', 'source_model' => '', 'note' => '', 'frontend_input_renderer' => '');
    // Now, overlay the incoming values on to the defaults.
    foreach ($values as $key => $newValue) {
        if (isset($data[$key]) == false) {
            echo "Attribute feature [{$key}] is not valid." . "<br/>";
            return false;
        } else {
            $data[$key] = $newValue;
        }
    }
    // Valid product types: simple, grouped, configurable, virtual, bundle, downloadable, giftcard
    $data['apply_to'] = $productTypes;
    $data['attribute_code'] = $attributeCode;
    $data['frontend_label'] = array(0 => $labelText, 1 => '', 3 => '', 2 => '', 4 => '');
    //<<<<
    //>>>> Build the model.
    $model = Mage::getModel('catalog/resource_eav_attribute');
    $model->addData($data);
    if ($setInfo !== -1) {
        $model->setAttributeSetId($setInfo['SetID']);
        $model->setAttributeGroupId($setInfo['GroupID']);
    }
    $entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
    $model->setEntityTypeId($entityTypeID);
    $model->setIsUserDefined(1);
    //<<<<
    // Save.
    try {
        $model->save();
    } catch (Exception $ex) {
        echo "Attribute [{$labelText}] could not be saved: " . $ex->getMessage() . "<br/>";
        return false;
    }
    if (is_array($options)) {
        foreach ($options as $_opt) {
            addAttributeValue($attributeCode, $_opt);
        }
    }
    $id = $model->getId();
    echo "Attribute [{$labelText}] has been saved as ID ({$id}).<br/>";
    // return $id;
}
 $sProduct->save();
 if ($parent_product_id == '') {
     $parent_product_id = $sProduct->getId();
 }
 /* Add or update child product */
 $subProductData = array();
 $pr_title = $data['Bezeichnung'] . " " . $data['Tragfaehigkeit'] . $data['ARSGI'];
 $sub_pr_sku = str_replace(array(' ', "/"), "_", $pr_title) . "_" . $data['ARHANR'];
 $short_size = $data['Bezeichnung'];
 $sub_product_id = Mage::getModel("catalog/product")->getIdBySku($sub_pr_sku);
 if ($sub_product_id == '') {
     $subProduct = Mage::getModel('catalog/product');
 } else {
     $subProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sub_pr_sku);
 }
 $subProductData = array('name' => $pr_title, 'sku' => $sub_pr_sku, 'description' => "Content for " . $pr_title, 'short_description' => "Short content for " . $pr_title, 'weight' => $data['ARGEW'], 'status' => 1, 'visibility' => '1', 'attribute_set_id' => 9, 'type_id' => 'simple', 'price' => $data['VK8'], 'store_id' => $storeID, 'category_ids' => $cats, 'website_ids' => $websiteIDs, 'tax_class_id' => 0, 'page_layout' => 'one_column', 'short_size' => $data['Bezeichnung'], 'load_capacity' => addAttributeValue('load_capacity', $data['Tragfaehigkeit']), 'speed_index' => addAttributeValue('speed_index', $data['ARSGI']), 'ean_number' => $data['AREAN'], 'article_code' => $data['ARHANR'], 'noise' => $data['LaermDB'], 'diameter' => addAttributeValue('diameter', $data['ARSZG']), 'width' => addAttributeValue('width', $data['Breite']), 'profile' => addAttributeValue('profile', $data['ARSQU']), 'fuel_efficiency' => addAttributeValue('fuel_efficiency', $data['Nasshaftung']), 'wet_grip' => addAttributeValue('wet_grip', $data['Rollwiderstand']));
 foreach ($subProductData as $key => $value) {
     $subProduct->setData($key, $value);
 }
 if ($sub_product_id == '') {
     $subProduct->setStockData(array('manage_stock' => 1, 'is_in_stock' => 1, 'qty' => $data['verfLB'], 'use_config_manage_stock' => 0));
 } else {
     $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($sub_product_id);
     $stockItemId = $stockItem->getId();
     $stockItem->setData('manage_stock', 1);
     $stockItem->setData('qty', (int) $data['verfLB']);
     $stockItem->save();
     $subProduct->setData('sku', $sub_pr_sku);
 }
 $subProduct->save();
 if ($sub_product_id == '') {
 } else {
     $subProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sub_pr_sku);
 }
 $range_arr = array('10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40-44', '45-49', '50-54', '55-59', '60-64', '65-69', '70-74');
 $et_range = '';
 for ($n = 0; $n < count($range_arr); $n++) {
     $exp_arr = explode('-', $range_arr[$n]);
     if ($exp_arr[0] <= trim($data['AREPTI']) && $exp_arr[1] >= trim($data['AREPTI'])) {
         //echo $exp_arr[0]."<=".trim($data['AREPTI'])."----".$exp_arr[1].">=".trim($data['AREPTI']);
         // echo "====>>>>";
         $et_range = $range_arr[$n];
         //echo "Range=".$range_arr[$n];
         break;
     }
 }
 $subProductData = array('name' => $pr_title, 'sku' => $sub_pr_sku, 'weight' => 10, 'status' => 1, 'visibility' => '1', 'attribute_set_id' => 4, 'type_id' => 'simple', 'price' => trim($data['ARVK4']), 'store_id' => $storeID, 'category_ids' => $cats, 'website_ids' => $websiteIDs, 'tax_class_id' => 0, 'page_layout' => 'one_column', 'short_size' => $short_size, 'center_hole' => trim($data['ARNABO']), 'et_measurement' => trim($data['AREPTI']), 'article_code' => trim($data['ARHANR']), 'rim_width' => addAttributeValue('rim_width', trim(str_replace(",", ".", $data['ARSBR']))), 'et_range' => attributeValueExists('et_range', $et_range), 'diameter' => addAttributeValue('diameter', trim($data['ARSZG'])), 'bolt_circle' => addAttributeValue('bolt_circle', trim($bolt_circle)));
 foreach ($subProductData as $key => $value) {
     $subProduct->setData($key, $value);
 }
 if ($sub_product_id == '') {
     $subProduct->setData('description', "Content for " . $pr_title);
     $subProduct->setData('short_description', "Short Content for " . $pr_title);
     $subProduct->setStockData(array('manage_stock' => 1, 'is_in_stock' => 1, 'qty' => trim($data['ARVLB']), 'use_config_manage_stock' => 0));
 } else {
     $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($sub_product_id);
     $stockItemId = $stockItem->getId();
     $stockItem->setData('manage_stock', 1);
     $stockItem->setData('qty', (int) $data['ARVLB']);
     $stockItem->save();
 }
 $subProduct->save();
<?php

require_once "lib/Magento.php";
$parseObj = new CSVParser();
$csv_directory = 'csv/attributes/';
$csv_file_name = $csv_directory . 'brands2jan16.csv';
$attribute_name = 'brands';
$item_rows = $parseObj->parseCSVFile($csv_file_name);
$arg_attribute = 'brand';
foreach ($item_rows as $item) {
    $brand = ucwords(strtolower($item['brands']));
    addAttributeValue($arg_attribute, $brand);
}
function addAttributeValue($arg_attribute, $arg_value)
{
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute = $attribute_model->load($attribute_code);
    $value['option'] = array($arg_value, $arg_value);
    $result = array('value' => $value);
    $attribute->setData('option', $result);
    $attribute->save();
    echo "Attribute '" . $arg_attribute . "' with \n        option '" . $arg_value . "' saved.<br />";
}