Hi, I would really appreciate if I could be helped on the following PHP code.

I have two arrays.
1) $user
2) $usertotal

$user can have as many names stored in it.

Example:
$user[0]="John";
$user[1]="Sean";
$user[2]="Nick";
$user[3]="Jay";
................
................

Similary,

$usertotal can have as many amount stored in it.

Example:
$usertotal[0]=100;
$usertotal[1]=60;
$usertotal[2]=150;
$usertotal[3]=0;
................
................

I am trying to build a finance related program where,
$user has spent $usertotal amount of money. For example in this case, John spent 100 dollars and Jay spent 0 dollars.

I also have a variable $average which is the average of all amount based on the number of users.

Now, what i am trying to do is display how much one user owes another user so that they all spend the same amount and be even with each other.

I am trying to compare each person's expense with $average and based on the difference the person should owe to another person.

For example, the output I want is something like this.

John owes $50 to Nick
Jay owes $100 to John
.....................
.....................

Suggestions and ideas would be highly appreciated. Thanks in advance.
I am a regular follower of Daniweb and this is my first question I thought I should really post.

hi,

Now, what i am trying to do is display how much one user owes another user so that they all spend the same amount and be even with each other.

How can they all spent same amount and be even?

For example, the output I want is something like this.

John owes $50 to Nick
Jay owes $100 to John

Isn't "John owes $50 to Nick" should be "Nick owes $25 to John" in order to be even. Or Didn't I understood anything you said :) ?

The examples above don't make mathematical sense to me as @niranga also points out.
The average total in your example is $77.50. Anybody who spend more than that is owed money and anybody who spent less owes money to the others. Is that the gist of what you are trying to do? Does it matter that everytime you add a new user the average will shift?

Member Avatar for diafol

Agreed, the logic behind this is all wrong. Getting the average from an array is simple. Owing money to others...?

So Jay gets money from everybody? 100 from John, 60 from Sean and 150 from Nick, giving him a whopping 310. So now Jay owes everybody 310, which he doesn't have of course, 930 not being in his money-grabbing pocket.

Come back with a revised idea of what you're trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.