Php 对象变量和第一个元素(foreach)

Php 对象变量和第一个元素(foreach),php,magento,variables,object,foreach,Php,Magento,Variables,Object,Foreach,我在magento中有一个变量: $collection = Mage::helper('catalog/product_compare')->getItemCollection(); $collection=Mage::helper('catalog/product_compare')->getItemCollection(); 现在只从我使用的foreach的第一个元素调用函数: foreach($id=>$comparing_product){$some_id=$comparing_pro

我在magento中有一个变量:

$collection = Mage::helper('catalog/product_compare')->getItemCollection(); $collection=Mage::helper('catalog/product_compare')->getItemCollection(); 现在只从我使用的foreach的第一个元素调用函数:

foreach($id=>$comparing_product){
$some_id=$comparing_product->getId();
break;
不使用foreach是否可以调用此函数

$id给我10,但我不能使用$product[0](或[10])(这不起作用,因为这个变量是object,对吗?

试试这个(我在ZF2集合上使用这个):

我找到了解决办法:

$collection->getFirstItem() $collection->getFirstItem()几乎不错:)我找到了解决方案:而不是first():getFirstItem()
$first = $collection->first();
$id = $first->getId();
$first = reset($collection);
$id = $first->getId();
$collection->getFirstItem()