php是一个简单的数学任务
我有一道最简单的数学题要做,我就是搞不懂(可能是整天工作累了)。很简单,我循环浏览各个项目,并希望显示最终价格,而不包括税费等。问题是我的数学是正确的,但是当我显示价格时,所有项目都具有相同的值(最后一个项目的值)。我知道每次循环时total变量都在变化,最后一个循环显示最后一个值。如何解决这个问题php是一个简单的数学任务,php,arrays,Php,Arrays,我有一道最简单的数学题要做,我就是搞不懂(可能是整天工作累了)。很简单,我循环浏览各个项目,并希望显示最终价格,而不包括税费等。问题是我的数学是正确的,但是当我显示价格时,所有项目都具有相同的值(最后一个项目的值)。我知道每次循环时total变量都在变化,最后一个循环显示最后一个值。如何解决这个问题 public function getTotal($items) { $total; foreach($items as $item){ $total = $item
public function getTotal($items)
{
$total;
foreach($items as $item){
$total = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;
}
它应该显示:
Item1: 154
Item2: 77
它显示:
Item1:77
Item2:77
每次迭代都会覆盖总变量。请尝试以下方法:
public function getTotal($items)
{
$total = 0;
foreach($items as $item){
$total += $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;
}
编辑:
我知道您希望看到每个产品的总数。您可以做的是返回一个数组而不是双精度数组。例如:
public function getTotal($items)
{
$total = array();
foreach($items as $item){
$total[] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;
}
每次迭代都会覆盖总变量。请尝试以下方法:
public function getTotal($items)
{
$total = 0;
foreach($items as $item){
$total += $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;
}
编辑:
我知道您希望看到每个产品的总数。您可以做的是返回一个数组而不是双精度数组。例如:
public function getTotal($items)
{
$total = array();
foreach($items as $item){
$total[] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;
}
您需要添加
[]
,这将返回它应该是什么样的数组列表
public function getTotal($items)
{
$total = array();
foreach($items as $item){
$total[] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;// returns an array;
}
或者可以作为
public function getTotal($items)
{
$total = array();
foreach($items as $key => $item){
$total[$key] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;// returns an array;
}
您需要添加
[]
,这将返回它应该是什么样的数组列表
public function getTotal($items)
{
$total = array();
foreach($items as $item){
$total[] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;// returns an array;
}
或者可以作为
public function getTotal($items)
{
$total = array();
foreach($items as $key => $item){
$total[$key] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $total;// returns an array;
}
我不知道您的全部代码是什么,所以我有一些猜测,但您是否考虑过使用类似于:
public function getTotal($item)
{
return $item->getPrice() - $item->getDiscount() + $item->getValue();
}
然后在您的方法中,您可以输出您的价格:
foreach( $items as $item )
{
// $item->getName() is an wild guess but still you get the point
echo $item->getName() . ': ' . $this->getTotal( $item );
}
另一种方法是将每个项目的总价存储在新数组中,然后返回:
public function getTotals($items)
{
$totals = array();
foreach( $items as $item )
{
$totals[ $item->getName() ] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $totals;
}
然后你就必须:
$totals = $this->getTotals( $items );
foreach( $totals as $name => $totalPrice )
{
echo $name . ': ' . $totalPrice;
}
我不知道您的全部代码是什么,所以我有一些猜测,但您是否考虑过使用类似于:
public function getTotal($item)
{
return $item->getPrice() - $item->getDiscount() + $item->getValue();
}
然后在您的方法中,您可以输出您的价格:
foreach( $items as $item )
{
// $item->getName() is an wild guess but still you get the point
echo $item->getName() . ': ' . $this->getTotal( $item );
}
另一种方法是将每个项目的总价存储在新数组中,然后返回:
public function getTotals($items)
{
$totals = array();
foreach( $items as $item )
{
$totals[ $item->getName() ] = $item->getPrice() - $item->getDiscount() + $item->getValue();
}
return $totals;
}
然后你就必须:
$totals = $this->getTotals( $items );
foreach( $totals as $name => $totalPrice )
{
echo $name . ': ' . $totalPrice;
}
$total+=$item->getPrice()-$item->get折扣()++$item->getValue()代码>^,不要忘记使用0
初始化$total
变量!是$total是一个数组不是我想要的。您的代码所做的是计算所有产品的总价。我想计算单个产品的总价格并显示它。在这种情况下添加方括号$total+=$item->getPrice()-$item->get折扣()+$item->getValue()代码>^,不要忘记使用0
初始化$total
变量!是$total是一个数组不是我想要的。您的代码所做的是计算所有产品的总价。我想计算单个产品的总价格并显示它。在这种情况下添加方括号,如果$items
为空怎么办?在我看来那应该包括你好。我理解你所做的,但那不是我想要的。此代码统计所有产品的总价。我想计算单个产品的总价。@walkingRed:如果$items
为空,将返回0,因为不会发生迭代。如果$items
为空,该怎么办?在我看来那应该包括你好。我理解你所做的,但那不是我想要的。此代码统计所有产品的总价。我想计算单个产品的总价。@walkingRed:如果$items
为空,将返回0,因为不会发生迭代。