The code logic for daily billing calculation (php) assumes that three people, A, B, and C, live together and need to develop A website to record their daily expenses, such as buying food. How do I design a data table? In addition, how can we calculate the total cost for each person?
The following is my code:
// $ Each array in aData indicates the daily consumption record, money: the total fee of the day, mean: the average fee, and others indicate the user name: 1/0/-1 payer/no need to pay/payer
$ AData = [
['Money' => 30, 'Mean '=> 10, 'twl' => 1, 'XXX' =>-1, 'yyy' =>-1],
['Money' => 10, 'Mean '=> 5, 'twl' =>-1, 'XXX' => 1, 'yyy' => 0],
];
$ Count = 0;
$ AList = [];
Foreach ($ aData as $ val ){
Foreach ($ val as $ k =>$ v ){
If ($ k = 'Money '){
$ Count + = $ val ['Money'];
} Elseif ($ k! = 'Mean '& $ k! = 'Boss '){
$ AList [$ k] [] = [
'Out' => $ v> 0? $ Val ['Money']: 0, // indicates the money you pay
'In' => $ v <0? $ Val ['Mean ']: 0, // indicates the money you want to share
'Who '=> $ val ['Boss'],
];
}
}
}
$ AData = [];
Foreach ($ aList as $ k => $ v ){
$ In = $ out = 0;
Foreach ($ v as $ key => $ val ){
$ In + = $ val ['in'];
$ Out + = $ val ['out'];
}
$ AData [$ k] = [
'Out' => $ out,
'In' => $ in,
];
}
The result is
In this way, you still cannot know who paid for it. Tutorial
Reply to discussion (solution)
No boss found in the data
Ideas:
User table, user consumption record table
Specific items are counted based on the items in the user consumption table.
User table
User_id
Username
Password
Consumption Table
Id
User_id user ID
Consumption_type
Money amount
Createtime time
In fact, I mean, what elements should be added to the array to Mark and calculate the amount of money each person should give to each other.
What does this mean?
$aData = [ ['money' => 30, 'mean'=> 10, 'twl' => 1, 'xxx' => -1, 'yyy' => -1], ['money' => 10, 'mean'=> 5, 'twl' => -1, 'xxx' => 1, 'yyy' => 0],];foreach($aData as $m) { $u = array_diff_key($m, ['money' => 0, 'mean' => 0]); arsort($u); foreach($u as $k=>$v) { if(!isset($r[$k])) $r[$k] = ['out' => 0, 'in' => 0, 'boss' => []]; if($v == 0) continue; if($v == 1) { $boss = $k; $r[$k]['out'] += $m['money']; }else { $r[$k]['out'] += $m['mean']; $r[$k]['boss'][] = $boss; $r[$boss]['in'] += $m['mean']; } }}print_r($r);
Array( [twl] => Array ( [out] => 35 [in] => 20 [boss] => Array ( [0] => xxx ) ) [yyy] => Array ( [out] => 10 [in] => 0 [boss] => Array ( [0] => twl ) ) [xxx] => Array ( [out] => 20 [in] => 5 [boss] => Array ( [0] => twl ) ))
This is almost the case,
Hypothesis
$ AData = [
['Money' => 30, 'Mean '=> 10, 'twl' => 1, 'XXX' =>-1, 'yyy' =>-1],
['Money' => 30, 'Mean '=> 10, 'twl' =>-1, 'XXX' =>-1, 'yyy' => 1],
['Money' => 10, 'Mean '=> 5, 'twl' =>-1, 'XXX' => 1, 'yyy' => 0],
['Money' => 20, 'Mean '=> 10, 'twl' => 0, 'XXX' => 1, 'yyy' =>-1],
['Money' => 20, 'Mean '=> 10, 'twl' => 0, 'XXX' => 1, 'yyy' =>-1],
['Money' => 20, 'Mean '=> 10, 'twl' => 0, 'XXX' => 1, 'yyy' =>-1],
];
It seems that the calculated data is not accurate.
You can calculate
$aData = [ ['money' => 30, 'mean'=> 10, 'twl' => 1, 'xxx' => -1, 'yyy' => -1], ['money' => 30, 'mean'=> 10, 'twl' => -1, 'xxx' => -1, 'yyy' => 1], ['money' => 10, 'mean'=> 5, 'twl' => -1, 'xxx' => 1, 'yyy' => 0], ['money' => 20, 'mean'=> 10, 'twl' => 0, 'xxx' => 1, 'yyy' => -1], ['money' => 20, 'mean'=> 10, 'twl' => 0, 'xxx' => 1, 'yyy' => -1], ['money' => 20, 'mean'=> 10, 'twl' => 0, 'xxx' => 1, 'yyy' => -1],];foreach($aData as $id=>$m) { $u = array_diff_key($m, ['money' => 0, 'mean' => 0]); arsort($u); foreach($u as $k=>$v) { if($v == 0) continue; if($v == 1) { $boss = $k; $r[$k][] = ['id' => $id, 'out' => $m['money']]; }else { $r[$k][] = ['id' => $id, 'out' => $m['mean'], 'boss' => $boss]; $r[$boss][] = ['id' => $id, 'in' => $m['mean'], 'boss' => $k]; } }}print_r($r);
Array( [twl] => Array ( [0] => Array ( [id] => 0 [out] => 30 ) [1] => Array ( [id] => 0 [in] => 10 [boss] => yyy ) [2] => Array ( [id] => 0 [in] => 10 [boss] => xxx ) [3] => Array ( [id] => 1 [out] => 10 [boss] => yyy ) [4] => Array ( [id] => 2 [out] => 5 [boss] => xxx ) ) [yyy] => Array ( [0] => Array ( [id] => 0 [out] => 10 [boss] => twl ) [1] => Array ( [id] => 1 [out] => 30 ) [2] => Array ( [id] => 1 [in] => 10 [boss] => xxx ) [3] => Array ( [id] => 1 [in] => 10 [boss] => twl ) [4] => Array ( [id] => 3 [out] => 10 [boss] => xxx ) [5] => Array ( [id] => 4 [out] => 10 [boss] => xxx ) [6] => Array ( [id] => 5 [out] => 10 [boss] => xxx ) ) [xxx] => Array ( [0] => Array ( [id] => 0 [out] => 10 [boss] => twl ) [1] => Array ( [id] => 1 [out] => 10 [boss] => yyy ) [2] => Array ( [id] => 2 [out] => 10 ) [3] => Array ( [id] => 2 [in] => 5 [boss] => twl ) [4] => Array ( [id] => 3 [out] => 20 ) [5] => Array ( [id] => 3 [in] => 10 [boss] => yyy ) [6] => Array ( [id] => 4 [out] => 20 ) [7] => Array ( [id] => 4 [in] => 10 [boss] => yyy ) [8] => Array ( [id] => 5 [out] => 20 ) [9] => Array ( [id] => 5 [in] => 10 [boss] => yyy ) ))
Thank you very much. Your ideas have helped me a lot.