How to Remove Last Item from Product Collection – Magento 1

collection;magento-1

I want to remove last item from product collection

$collection = Mage::getModel('catalog/product')->getCollection();

But I have to avoid any 'loop' solutions.

Best Answer

Without context, I cannot say if this is a good idea, but if you have a loaded collection and want to remove the last item from the collection (not delete it), you can do this:

$lastId = $collection->getLastItem()->getId();
$collection->removeItemByKey($lastId);

This works because internally the items are stored in an array with their id as key.

Related Topic