php 中的关联数组值 +=
Associative array value += in php
我有一个发票项目清单。我创建了一个税收分解数组(关联数组)来存储唯一的税收百分比及其税额。
$taxbreakup=array();
@foreach($invoiceinfo->invoiceitems as $invoice_items)
$taxbreakup[$invoice_items->tax_percentage]+=$invoice_items->tax_amount;
@endforeach
组税百分比并显示特定税百分比的总税额
@foreach($taxbreakup as $key => $value)
<tr>
<td colspan="12"></td>
<td class="btmlft" colspan="2">GST ({{number_format($key,2)}})%</td>
<td class="btmlft rghtbrd alignright">{{number_format(($value),2) }}</td>
</tr>
@endforeach
我遇到了一个未定义的索引错误。
我认为这个变量 $taxbreakup 是空的,检查 $taxbreakup 是否为空,然后执行 foreach
为了使用+=
,您要将结果累加到其中的变量必须进行初始化,在您的情况下,它尚不存在且未进行初始化。
就算你想做
$arr['val'] = $arr['val'] + 1;
需要创建接收变量。
所以我会用
$taxbreakup=array();
@foreach($invoiceinfo->invoiceitems as $invoice_items)
// check accumulator exists before using it
if ( ! isset($taxbreakup[$invoice_items->tax_percentage]) ) {
$taxbreakup[$invoice_items->tax_percentage] = 0;
}
$taxbreakup[$invoice_items->tax_percentage] += $invoice_items->tax_amount;
@endforeach
if ( ! isset($arr['hello']) ) { $arr['hello'] = 0; }
我有一个发票项目清单。我创建了一个税收分解数组(关联数组)来存储唯一的税收百分比及其税额。
$taxbreakup=array();
@foreach($invoiceinfo->invoiceitems as $invoice_items)
$taxbreakup[$invoice_items->tax_percentage]+=$invoice_items->tax_amount;
@endforeach
组税百分比并显示特定税百分比的总税额
@foreach($taxbreakup as $key => $value)
<tr>
<td colspan="12"></td>
<td class="btmlft" colspan="2">GST ({{number_format($key,2)}})%</td>
<td class="btmlft rghtbrd alignright">{{number_format(($value),2) }}</td>
</tr>
@endforeach
我遇到了一个未定义的索引错误。
我认为这个变量 $taxbreakup 是空的,检查 $taxbreakup 是否为空,然后执行 foreach
为了使用+=
,您要将结果累加到其中的变量必须进行初始化,在您的情况下,它尚不存在且未进行初始化。
就算你想做
$arr['val'] = $arr['val'] + 1;
需要创建接收变量。
所以我会用
$taxbreakup=array();
@foreach($invoiceinfo->invoiceitems as $invoice_items)
// check accumulator exists before using it
if ( ! isset($taxbreakup[$invoice_items->tax_percentage]) ) {
$taxbreakup[$invoice_items->tax_percentage] = 0;
}
$taxbreakup[$invoice_items->tax_percentage] += $invoice_items->tax_amount;
@endforeach
if ( ! isset($arr['hello']) ) { $arr['hello'] = 0; }