Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于日常收费计算的代码逻辑(php)

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

Jun 20, 2016 pm 12:45 PM

假设,有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);
Copy after login
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                )        ))
Copy after login

差不多是这样,
假设
$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);
Copy after login
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                )        ))
Copy after login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles