Hi,
I have a the following tax table which shows tax calculations on wages. The way it works is that you find the row where your annual wage is between the lower and upper limits. Then the difference between your wage and the above column is multiplied by the rate. You then add the last column to your result.

Lower	 Upper	 Percent	 Above	 Also Add
0	 2,650 	0.0%	0	0
 2,650 	 10,120 	10.0%	 2,650 	0
 10,120 	 33,520 	15.0%	 10,120 	 747.00 
 33,520 	 77,075 	25.0%	 33,520 	 4,257.00 
 77,075 	 162,800 	28.0%	 77,075 	 15,145.75 
 162,800 	 351,650 	33.0%	 162,800 	 39,148.75 
 351,650 	 and over 	35.0%	 351,650 	 101,469.25 

Note: Lower limit = last upper limit;
above = last lower limit;
also add = (last upper - lower)*last percent.

Example: Say you earn 15,600 a year, you take:
(15,600-10,120) * .15 + 747 = 1,569

My question is, how can you do this in PHP given these arrays:

$LowerLimits = array(0,2650,10120,33520,77075,162800,351650);  //must you have a 0?
$Percents = array(0,.10,.15,.25,.28,.33,.35);

Thank you

    Here's the code I've been working with.... the additional amount doesn't work. Also, I don't think I made it all too efficient.

    //Example wage
    $wage = 36400;
    
    
    $LowerLimits = array(0,2650,10120,33520,77075,162800,351650);
    $Percents = array(0,.10,.15,.25,.28,.33,.35);
    
    //Figure out which row in the table
    $count = 0;
    foreach ($LowerLimits as $value) { 
        if ($Wage < $value) { 
            $UseKey = $count; 
            break; 
        } 
    $count++;
    } 
    
    //Calculate the Additional amount
    //
    //DOESNT WORK
    //
    $AddAmount = 0;
    if ($UseKey > 1) { 
    	$i=0;
    	while($i<=$UseKey)  {
    	  $AddAmount = $AddAmount + ($LowerLimits[$UseKey-1]-$LowerLimits[$UseKey-2])*$Percents[$UseKey-2];
    	  $i++;
    	}
    }
    
    //Create output
    $Tax = ($Wage-$LowerLimits[$UseKey-1])*$Percents[$UseKey-1] + $AddAmount;
    $Tax = round($Tax, 2);
    Echo "Expect to pay $" . $Tax . " in taxes";
    

      I would have them stored in my MySQL database and use the "between" funtion.

      This function allows you to search for values between 2 certain values, this will make your task easier...

      http://newsourcemedia.com/home.php?view=82

      Not so, you will have to write a function that loops through value array and uses simple >< comparison.

        If you store the values in a db then you can do the whole calculation in a single query that returns the required figure.

        However, your question was how to do it in php.

        $uppers = array(2650=>array(0,0,0),10120=>array(10,2650,0),33520=>array(15,10120,747.00),77075=>array(25,33520,4257.00),// etc
        foreach ($uppers as $key=>$val) {
          if ($wage < $key) {
            $percent = $val[0];
            $above = $val[1];
            $add = $val[2];
            break;
           }
        }
        // do your calculation now
        

        Now, I don't see that an array is your best solution for this since you are just going to have to traverse the array anyway. Personally I would use a SWITCH to find the required set of values as I find it more legible.

          Write a Reply...