Magento Update on The Fly The Super Product Attributes Configuration

<?php

require_once 'app/Mage.php';
umask(0);
Mage::app("admin");
ini_set("display_errors", 0);

/** This file will update The Super product attributes configuration
 *  The price for each color will be calculated based on the price
 *  of the simple product (PSP) and the price of the configurable product (PCP)
 *  price is  PSP - PCP (PCP < PSP)
 *  Need help contact us at:  support@ayasoftware.com
 *  author : EL HASSAN MATAR
 */
$model = Mage::getModel("catalog/product"); 
$products = $model->getCollection();
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('visibility', 4);//catalog, search
$products->addAttributeToSelect('*');
$prodIds=$products->getAllIds();
$product = Mage::getModel('catalog/product');

foreach($prodIds as $productid) {

/**
* Load Product you want to get Super attributes of
*/
	$product = Mage::getSingleton("catalog/Product")->load($productid);

	$configurablePrice = $product->getPrice();

	$associatedProducts=$product->getTypeInstance()->getUsedProducts();
	$stack = array();
	for($j=0; $j< sizeof($associatedProducts) ; $j++){
		array_push($stack,  Array("color"=>$associatedProducts[$j]['color'],"size"=>$associatedProducts[$j]['size'], "price"=>$associatedProducts[$j]['price']));
	}

	if ($data = $product->getTypeInstance()->getConfigurableAttributesAsArray(($product))) {

		foreach ($data as $attributeData) {

			$id = isset($attributeData['id']) ? $attributeData['id'] : null;
			$size = sizeof($attributeData['values']);
			for($j=0; $j< $size ; $j++){
				multiArrayValueSearch($stack, $attributeData['values'][$j]['value_index'], $match);
				reset($match); // make sure array pointer is at first element
				$firstKey = key($match);
				$match= array();
				$attributeData['values'][$j]['pricing_value'] = $stack[$firstKey]['price'] - $configurablePrice;
			}

			if($id == 7){   // Check your $id value
				$attribute = Mage::getModel('catalog/product_type_configurable_attribute')
				->setData($attributeData)
				->setId($id)
				->setStoreId($product->getStoreId())
				->setProductId($productid)
				->save();
			}
		}
	}
}
function multiArrayValueSearch($haystack, $needle, &$result, &$aryPath=NULL, $currentKey='') {
	if (is_array($haystack)) {
		$count = count($haystack);
		$iterator = 0;
		foreach($haystack as $location => $straw) {
			$iterator++;
			$next = ($iterator == $count)?false:true;
			if (is_array($straw)) $aryPath[$location] = $location;
			multiArrayValueSearch($straw,$needle,$result,$aryPath,$location);
			if (!$next) {
				unset($aryPath[$currentKey]);
			}
		}
	} else {
		$straw = $haystack;
		if ($straw == $needle) {
			if (!isset($aryPath)) {
				$strPath = "\$result[$currentKey] = \$needle;";
			} else {
				$strPath = "\$result['".join("']['",$aryPath)."'][$currentKey] = \$needle;";
			}
			eval($strPath);
		}
	}
}
?>