问题描述
我使用CODEIGNITER购物车类。以下是购物车内容的 VAR_DUMP
。我有一个无线电选择字段,允许您选择 SHIP
选项。问题是,我的PHP没有得到 SHIP
值,但只返回购物车值中的最后一个项目,乘以购物车项目的总数。如何从每个项目中获取 SHIP
数组并对值进行求和?
I'm using CODEIGNITER Cart Class. The following is a VAR_DUMP
of the cart contents. I have a radio selection field which allows you to select a SHIP
option. The problem is that my PHP is not getting the SHIP
values but only returning the last item in cart's value and multiplying by the total number of cart items. How do I get the SHIP
array from each item and sum the values?
$ship_array = $this->cart->ship($item['rowid']);
foreach ($ship_array as $index => $ship_option){
#SHIPPING CALC
$ship_total = 0;
foreach ($cart as $item)
{
$ship_total += $ship_option;
}
echo '<label><input type="radio" name="print" class="option" data-number="'. $ship_total .'" value="'. $index .'"/> UPS '. $index .' </label> '; }
^此代码只能从 ship
在购物车的最后一项。结果将表明 ground
运费是$ 43.35(14.45(地运价值* 3总项)
^this code will only get the values from ship
in the last item in the cart. The result will say that ground
shipping is $43.35 (14.45 (ground shipping value * 3 total items)
是错误的,我需要从每个 $ item
Obviously this is wrong i need to add the shipping values from each $item
array (size=3)
'554ed09a5f80917741359cc9da50a75c' =>
array (size=8)
'rowid' => string '554ed09a5f80917741359cc9da50a75c' (length=32)
'id' => string '101' (length=3)
'qty' => string '1' (length=1)
'price' => string '112.5' (length=5)
'name' => string 'Business Card' (length=13)
'ship' =>
array (size=3)
'Ground' => float 9.73
'2nd Day Air' => float 18.54
'Overnight' => float 26.27
'options' =>
array (size=2)
'Print Package' => string 'Pro' (length=3)
'Design Package' => string 'Premium' (length=7)
'subtotal' => float 112.5
'675d8a197a25e6720af7ac05707fee40' =>
array (size=8)
'rowid' => string '675d8a197a25e6720af7ac05707fee40' (length=32)
'id' => string '102' (length=3)
'qty' => string '1' (length=1)
'price' => string '446' (length=3)
'name' => string 'Booklet' (length=7)
'ship' =>
array (size=3)
'Ground' => float 14.45
'2nd Day Air' => float 22.57
'Overnight' => float 34.12
'options' =>
array (size=2)
'Print Package' => string 'Plus' (length=4)
'Design Package' => string 'Ultimate' (length=8)
'subtotal' => int 446
'aea5d2f84151e65a0a1e50371aad26aa' =>
array (size=8)
'rowid' => string 'aea5d2f84151e65a0a1e50371aad26aa' (length=32)
'id' => string '102' (length=3)
'qty' => string '1' (length=1)
'price' => string '325' (length=3)
'name' => string 'Booklet' (length=7)
'ship' =>
array (size=3)
'Ground' => float 14.45
'2nd Day Air' => float 22.57
'Overnight' => float 34.12
'options' =>
array (size=2)
'Print Package' => string 'Pro' (length=3)
'Design Package' => string 'Premium' (length=7)
'subtotal' => int 325
推荐答案
您可以尝试
$array = array(
'554ed09a5f80917741359cc9da50a75c' => array('rowid' => '554ed09a5f80917741359cc9da50a75c','id' => '101','qty' => '1','price' => '112.5','name' => 'Business Card','ship' => array('Ground' => 9.73,'2nd Day Air' => 18.54,'Overnight' => 26.27),'options' => array('Pr Package' => 'Pro','Design Package' => 'Premium','subtotal' => 112.5)),
'675d8a197a25e6720af7ac05707fee40' => array('rowid' => '675d8a197a25e6720af7ac05707fee40','id' => '102','qty' => '1','price' => '446','name' => 'Booklet','ship' => array('Ground' => 14.45,'2nd Day Air' => 22.57,'Overnight' => 34.12),'options' => array('Pr Package' => 'Plus','Design Package' => 'Ultimate','subtotal' => 446)),
'aea5d2f84151e65a0a1e50371aad26aa' => array('rowid' => 'aea5d2f84151e65a0a1e50371aad26aa','id' => '102','qty' => '1','price' => '325','name' => 'Booklet','ship' => array('Ground' => 14.45,'2nd Day Air' => 22.57,'Overnight' => 34.12),'options' => array('Pr Package' => 'Pro','Design Package' => 'Premium','subtotal' => 325)));
$shipArray = $array ; // Replace with $this->cart->ship($item['rowid']);
$shipTotal = array("prize"=>0,"ground"=>0,"air"=>0,"overnight"=>"0");
foreach ($shipArray as $index => $shipOption){
$shipTotal['prize'] += $shipOption['qty'] * $shipOption['price'];
$shipTotal['ground'] += $shipOption['ship']['Ground'];
$shipTotal['air'] += $shipOption['ship']['2nd Day Air'];
$shipTotal['overnight'] += $shipOption['ship']['Overnight'];
}
var_dump($shipTotal);
输出
array
'prize' => float 883.5
'ground' => float 38.63
'air' => float 63.68
'overnight' => float 94.51
这篇关于如何添加多分区数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!