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

WBOY
Release: 2016-06-13 12:26:52
Original
1079 people have browsed it

关于日常收费计算的代码逻辑(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,
];
}

结果是

这样还是不能知道 到底谁付钱给谁。求教
------解决思路----------------------
你可以一笔一笔的算

$aData = [<br /> ['money' => 30, 'mean'=> 10, 'twl' => 1, 'xxx' => -1, 'yyy' => -1],  <br />  ['money' => 30, 'mean'=> 10, 'twl' => -1, 'xxx' => -1, 'yyy' => 1], <br /> ['money' => 10, 'mean'=> 5, 'twl' => -1, 'xxx' => 1, 'yyy' => 0],<br /> ['money' => 20, 'mean'=> 10, 'twl' => 0, 'xxx' => 1, 'yyy' => -1],<br /> ['money' => 20, 'mean'=> 10, 'twl' => 0, 'xxx' => 1, 'yyy' => -1],<br /> ['money' => 20, 'mean'=> 10, 'twl' => 0, 'xxx' => 1, 'yyy' => -1],<br />];<br />foreach($aData as $id=>$m) {<br />  $u = array_diff_key($m, ['money' => 0, 'mean' => 0]);<br />  arsort($u);<br />  foreach($u as $k=>$v) {<br />    if($v == 0) continue;<br />    if($v == 1) {<br />      $boss = $k;<br />      $r[$k][] = ['id' => $id, 'out' => $m['money']];<br />    }else {<br />      $r[$k][] = ['id' => $id, 'out' => $m['mean'], 'boss' => $boss];<br />      $r[$boss][] = ['id' => $id, 'in' => $m['mean'], 'boss' => $k];<br />    }<br />  }<br />}<br />print_r($r);
Copy after login
Array<br />(<br />    [twl] => Array<br />        (<br />            [0] => Array<br />                (<br />                    [id] => 0<br />                    [out] => 30<br />                )<br /><br />            [1] => Array<br />                (<br />                    [id] => 0<br />                    [in] => 10<br />                    [boss] => yyy<br />                )<br /><br />            [2] => Array<br />                (<br />                    [id] => 0<br />                    [in] => 10<br />                    [boss] => xxx<br />                )<br /><br />            [3] => Array<br />                (<br />                    [id] => 1<br />                    [out] => 10<br />                    [boss] => yyy<br />                )<br /><br />            [4] => Array<br />                (<br />                    [id] => 2<br />                    [out] => 5<br />                    [boss] => xxx<br />                )<br /><br />        )<br /><br />    [yyy] => Array<br />        (<br />            [0] => Array<br />                (<br />                    [id] => 0<br />                    [out] => 10<br />                    [boss] => twl<br />                )<br /><br />            [1] => Array<br />                (<br />                    [id] => 1<br />                    [out] => 30<br />                )<br /><br />            [2] => Array<br />                (<br />                    [id] => 1<br />                    [in] => 10<br />                    [boss] => xxx<br />                )<br /><br />            [3] => Array<br />                (<br />                    [id] => 1<br />                    [in] => 10<br />                    [boss] => twl<br />                )<br /><br />            [4] => Array<br />                (<br />                    [id] => 3<br />                    [out] => 10<br />                    [boss] => xxx<br />                )<br /><br />            [5] => Array<br />                (<br />                    [id] => 4<br />                    [out] => 10<br />                    [boss] => xxx<br />                )<br /><br />            [6] => Array<br />                (<br />                    [id] => 5<br />                    [out] => 10<br />                    [boss] => xxx<br />                )<br /><br />        )<br /><br />    [xxx] => Array<br />        (<br />            [0] => Array<br />                (<br />                    [id] => 0<br />                    [out] => 10<br />                    [boss] => twl<br />                )<br /><br />            [1] => Array<br />                (<br />                    [id] => 1<br />                    [out] => 10<br />                    [boss] => yyy<br />                )<br /><br />            [2] => Array<br />                (<br />                    [id] => 2<br />                    [out] => 10<br />                )<br /><br />            [3] => Array<br />                (<br />                    [id] => 2<br />                    [in] => 5<br />                    [boss] => twl<br />                )<br /><br />            [4] => Array<br />                (<br />                    [id] => 3<br />                    [out] => 20<br />                )<br /><br />            [5] => Array<br />                (<br />                    [id] => 3<br />                    [in] => 10<br />                    [boss] => yyy<br />                )<br /><br />            [6] => Array<br />                (<br />                    [id] => 4<br />                    [out] => 20<br />                )<br /><br />            [7] => Array<br />                (<br />                    [id] => 4<br />                    [in] => 10<br />                    [boss] => yyy<br />                )<br /><br />            [8] => Array<br />                (<br />                    [id] => 5<br />                    [out] => 20<br />                )<br /><br />            [9] => Array<br />                (<br />                    [id] => 5<br />                    [in] => 10<br />                    [boss] => yyy<br />                )<br /><br />        )<br /><br />)<br /><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template