时间:2021-07-01 10:21:17 帮助过:14人阅读
在数据里没有看到 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 ) ))
嗯,非常感谢。你的思路给了我很大的帮助。