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

关于日常收费计算的代码逻辑(php),该怎么处理

程序员文章站 2022-04-01 13:43:05
...
关于日常收费计算的代码逻辑(php)
假设,有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,
];
}

结果是
关于日常收费计算的代码逻辑(php),该怎么处理
这样还是不能知道 到底谁付钱给谁。求教
------解决思路----------------------
你可以一笔一笔的算
$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
)

)

)

关于日常收费计算的代码逻辑(php),该怎么处理

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频