Php Division remainders & Arrays -


i have array $blocks has 4 items constant values a, b, c, d

$blocks["a"] = 20; $blocks["b"] = 1000; $blocks["c"] = 10000; $blocks["d"] = 50000; 

another function returns value, lets 358020 (it's high can drop few tens

how write function take value , return array of how many of each item exists.

example output like:

 $output["a"] = 1;  $output["b"] = 3;  $output["c"] = 0;  $output["d"] = 7; 

starting largest block, how many of block fits value, remainder passed next largest, , on...

calculatenumbercredits(25000);  function calculatenumbercredits($experience) {      # credits have     $credits = array(         'a' => '10000',         'b' => '2000',         'c' => '1000',     );      # keep track of amount needed per credit     $timescreditneeded = array();      # start calculating amount per credit need     foreach($credits $creditid => $creditamount) {          # 1) calculate number of times amount fits within amount of experience         $times = floor($experience / $creditamount);          # 2) calculate remainder of above division en cache next     calculation         $experience = $experience % $creditamount;          # 3) cache number of times credit fits within experience         $timescreditneeded[$creditid] = $times;      }      echo '<pre>';     print_r($timescreditneeded);      return $timescreditneeded;  }  // return array ( [a] => 2 [b] => 2 [c] => 1 ) 

i loop through credits have in system. in example credits order high low. when not case should order them te desired result.

1) each credit try find max number of times credit fits within experience of particular user. because floatnumber make no sense floor() result of division.

2) after found number of times credit fits, calculate remainder next iteration (next credit). can find remainder calculating modulus.

3) last not least cache number of times credit fits.

hope helps!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -