如何从此数组中获取 cost_measure ==" Percent" 的记录,然后仅迭代这些记录。
这是我拥有的数组:
array (size=5)
0 =>
array (size=8)
'cno' => string 'C-01' (length=4)
'cost_name' => string 'Trošak prijevoz' (length=16)
'cost_description' => string 'Trošak prijevoza od Husvarne do K+N' (length=36)
'cost_type' => string 'Transport' (length=9)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.50' (length=4)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
1 =>
array (size=8)
'cno' => string 'C-02' (length=4)
'cost_name' => string 'Carina' (length=6)
'cost_description' => string 'Carina na robu koja se uvozi van EU' (length=35)
'cost_type' => string 'Customs' (length=7)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.00' (length=4)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
2 =>
array (size=8)
'cno' => string 'C-03' (length=4)
'cost_name' => string 'Banka' (length=5)
'cost_description' => string 'Troškovi banke za obradu transakcija' (length=37)
'cost_type' => string 'Bank' (length=4)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.15' (length=4)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
3 =>
array (size=8)
'cno' => string 'C-04' (length=4)
'cost_name' => string 'PDV' (length=3)
'cost_description' => string 'Trošak poreza za državu' (length=25)
'cost_type' => string 'VAT' (length=3)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '25.00' (length=5)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
4 =>
array (size=8)
'cno' => string 'C-06' (length=4)
'cost_name' => string 'Test cost' (length=9)
'cost_description' => string 'This one is to test the change' (length=30)
'cost_type' => string 'Main' (length=4)
'cost_measure' => string 'Absolute' (length=8)
'cost_amount' => string '120.00' (length=6)
'cost_vcfc' => string 'Fixed' (length=5)
'custom_cost' => int 1
获得带有cost_measure =="百分比"的记录后我需要将这些百分比添加到$ number。例如:
$number + cost_amount 0.50 %
$number + cost_amount 0.15 %
$number + cost_amount 25 %
答案 0 :(得分:1)
不确定你想要什么(值或字符串)。尝试这个:
$arr=array()//your array...
$number=0;$numberText="";
foreach($arr as $key=>$value){
if(strcmp($value["cost_measure"],"Percent") == 0){
$number = $number+$value["cost_amount"];
$numberText.="cost_amount ".$value["cost_amount"]." %<br>";
}
}
echo $number;//total percentage added.
echo $numberText;//string with all the percentages