1

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 %
1
  • do you want to display the string like in example?. Commented Feb 2, 2017 at 10:30

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

OK this is not working properly. It adds up 120% and it should add up 25%, 0.50% and 0.15% from array and each separatly like this: $number*0.50%, $number*0.15% and $number*25% Not summing.
OK, I see where the problem is. It's not getting only Percent it gets all $value["cost_measure"]. That's why it's getting too big. It's getting Absolute also. Something is wrong with if(strcmp($value["cost_measure"],"Percent") == 0){
OK, I've got it. Revorked you're code a little and it's right what I wanted! Thank you so much for you're help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.