欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

关于日常收费计算的代码逻辑(php)

程序员文章站 2022-05-20 15:17:45
...
假设,有A,B,C三个人住在一起,需要开发一个网站来记录他们日常花费,比如买菜等等。那么数据表该怎么设计呢?另外这些数据怎么运算到每个人总共要付出多少钱呢?
以下是我的代码:
//$aData里面的每个数组表示每天的消费记录, money:当天总费用, mean:人均费用 ,其他的表示 用户名 : 1/0/-1 付钱人/不用付/要付钱人
$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, //表示自己付出的钱
'in' => $v '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,
];
}

结果是

这样还是不能知道 到底谁付钱给谁。求教


回复讨论(解决方案)

在数据里没有看到 boss

思路:
用户表,用户消费记录表
具体统计什么项目,根据用户消费表的项目对应。

user表
user_id
username
password

consumption表
id
user_id 用户id
consumption_type 消费类型:如:电费,买菜,坐车
money 金额
createtime 时间

其实我的意思是,要在数组里再添加 什么元素来标记 并计算 每个人相互之间应该给出多少钱 给谁

这个意思?

$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                )        ))

差不多是这样,
假设
$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],
];

感觉算出来的数据又不太准了

你可以一笔一笔的算

$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                )        ))

嗯,非常感谢。你的思路给了我很大的帮助。