Website ID from Store Group ID in Magento

I am posting here code for fetching website information from Store Group Id.
Also useful code showing relations between Stores, Groups and Websites.


< ?php 
$store_id = 1; //your required store Id will go here
$store_model = Mage::getModel('core/store'); //store model
$store_group_model = Mage::getModel('core/store_group'); //store group model
$website_model = Mage::getModel('core/website'); //website model
$store_data =  $store_model->load($store_id); //load store object
$group_id = $store_data->getGroupId(); //get store group id from the store   
$website_id = $store_data->getWebsiteId();   //get website id from the store         
         
$website_id = $store_group_model->load($group_id)->getWebsiteId();
//get website id from the store group id       
$website_obj = $store_group_model->load($group_id)->getWebsite();
//get website object
$group_ids = $website_obj->getGroupIds();
//get all group ids as array of the website
$stores = $store_model->getCollection()->addGroupFilter($group_id);  
//get the stores from the existing store group          
foreach ($stores as $_store): 
    $store_ids[] = $_store->getId(); //get store id                  
endforeach;
     
$stores = $store_model->getCollection()->addWebsiteFilter($website_id);
 //get the stores from the existing website
foreach ($stores as $_store): 
    $store_ids[] = $_store->getId();                 
endforeach;
$store_ids = $website_model->load($website_id)->getStoreIds();
 //get the stores array from the website id
         
?>

Comments