【发布时间】:2015-04-24 21:07:30
【问题描述】:
我创建了一个计算项目总价格的树枝扩展函数:
public function getTotal($items)
{
$total = array();
foreach($items as $item){
$total[] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;
}
但是,当我显示它们时,我得到 error: array to string conversion
我是这样显示的:
{{getTotalTax(product)}}
我试过这样做没有帮助:
public function getTotal($items)
{
$totals = array();
foreach($items as $item){
$totals[] = $item->getPrice() - $item->getDiscount() + $item->getValue();
if(is_array($totals)) {
return '';
}
}
return $totals;
}
【问题讨论】:
-
您返回一个数组
$totals,稍后您将尝试打印该数组。也许您可以不使用数组,而是执行以下操作:$total += ... -
你好。我想要的是单独显示每个产品的总价格,这就是我需要一个数组的原因。如果我这样做 += 它只会打印所有产品的总和
-
为什么不在你的产品实体中添加一个方法,比如“getTotalPrice()”,它会总结项目的折扣和价值?