日次料金計算のコード ロジック (php)
A、B、C という 3 人が一緒に住んでおり、食料品の購入など、彼らの毎日の出費を記録するための Web サイトを開発する必要があると仮定します。では、データテーブルはどのように設計すればよいのでしょうか?さらに、これらのデータは各人が支払わなければならない合計金額をどのように計算するのでしょうか?
以下は私のコードです:
//$aData の各配列は毎日の消費記録、金額: その日の総コスト、平均: 1 人当たりのコスト、その他の表現を表します。 ユーザー名: 1/0 / -1 支払う人/支払う必要がない人
$aData = [
['money' => 30, 'mean'=> 10, 'twl' => 1, 'xxx' => -1, 'yyy' => -1],
['お金' => 5, 'twl' => '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' => $val[ 'money'] : 0, //支払わなければならないお金を示します
'in' => $val['mean'] : 0, //共有したいお金を示します
'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,
];
}
結果は
このようにしても、誰が誰に支払ったのかを知ることはできません。アドバイスを求めています
-----解決策のアイデア----------------------
あなたならそれができます1 カウントずつ
$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);
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 />