arrays - Who owes what PHP script -
i tried make owes script in php
, it's harder put in code thought.
i have made example of 5 people below:
person paid owes/receivables 480 -26 b 600 94 c 1200 694 d 250 -256 e 0 -506 total 2530 average owe per person = 506
what trying achieve, script prints out how each person owes, , whom. first approach split people owed money in array
, , same people needs paid in array
. confused approach still, can't explain afterwards.
this rules of script:
- arrange values of
arrays
highest first. - subtract highest receivable highest owes array.
- if receivables value still positive, move on next value in owes array.
- if receivables value negative following: take negative value , subtract original owed, subtract value on receivable array (will 0 on value then). move on next on receivable array.
not sure if makes sense or not. hoping can clarify this.
i've made 2 arrays , thought maybe while or foreach loop values , if sentences it, pretty confused @ point.
you have average value payed each person, have receivable , owed values of each person, need find way distribute value owed evenly among people have receivable value.
using "fair" algorithm , example, value peron e owes person c calculated such:
$valueowedbye * $valuereceivablebyc / $totalreceivablevalue
this gives weighted distribution of owed value among people have receivable values. using algorithm receivable values should reach 0 @ same time , people higher receivable values bigger part of debt.
ofcourse use different algorithm. decide on algorithm want use before try implement it, try make mathematical model of it, becomes trivial implement.
Comments
Post a Comment