Reputation: 39
How can I only get records which have cost_measure == "Percent" from this array and then iterate over those records only.
This is array I have:
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
After I get records with cost_measure == "Percent" I need to add these percentage to $number. For example:
$number + cost_amount 0.50 %
$number + cost_amount 0.15 %
$number + cost_amount 25 %
Upvotes: 1
Views: 41
Reputation: 11859
Not sure what you want(value or string).try this:
$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
Upvotes: 1