title: How to assign categories for products in magento Programmatically
body: <p>I am a newbie in magento. Basically, I want to assign multiple products to multiple categories. I have followed <a href="https://stackoverflow.com/questions/10523254/magento-mass-assign-products-to-category">this post</a> and I have done the following code which is working fine:</p>
  
  <pre><code>   $collection = Mage::getModel('catalog/product')-&gt;getCollection();//my coustom collection
          $categorys_ids = array(1,2,3,4,5);//Array of ids etc 
          if ($categorys_ids != NULL &amp;&amp; $collection-&gt;getData()!= NULL)
              {
                  foreach ($collection as $product) 
                  {
                          $categories_pd = $product-&gt;getCategoryIds();                              
                          $product-&gt;setCategoryIds(array_merge($product-&gt;getCategoryIds(),array($categorys_ids)));
                          $product-&gt;save();
                  }
              }
  </code></pre>
  
  <p>Now, the main issue is that when I assign set category id for the products it takes a lot of time. I have 200 products and this takes up to two minutes or so, which is a lot of time. </p>
  
  <p>I was wondering if there is a way that I can assign categories to a products array instead of assigning products to categories or something that can be optimized and take less time.</p>