我有以下代码,它试图根据产品的创建日期对产品数组进行排序:
private function sortProductsByDate(Product $a, Product $b)
{
if ($a->getCreated() == $b->getCreated()) {
return 0;
}
return ($a->getCreated() < $b->getCreated()) ? -1 : 1;
}
/**
* Get the most 4 recent items
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMostRecentItems()
{
$userMostRecentItems = array();
$products = $this->getProducts();
usort($products, "sortProductsByDate");
foreach ($this->getProducts() as $product) {
ladybug_dump($product->getCreated());
}
$mostRecentItems = $this->products;
return $this->isLocked;
}为什么这会给我带来这个错误:
Warning: usort() expects parameter 1 to be array, object given 想法?
发布于 2013-09-10 01:01:13
我猜getProducts()返回一个\Doctrine\Common\Collections\Collection (很可能是一个ArrayCollection)。使用
$products = $this->getProducts()->getValues();你也会想要用
usort($products, array($this, 'sortProductsByDate'));最后,在您的$products中使用foreach数组
foreach ($products as $product)https://stackoverflow.com/questions/18709184
复制相似问题