当前位置:网站首页>php中计算树状结构数据中的合计
php中计算树状结构数据中的合计
2022-07-02 06:23:00 【夜空の雪風】
前言
在项目开发的过程中有时候遇到树状结构数据计算从最末级节点依次计算到根节点中所有数据值的和,也就是计算子级节点数据相加等于父级节点,以此类推到根节点。
下面的方法使用到了递归查询计算的方法。
树状结构数据示例:
$tree = [
0 => [
"Code" => "1",
"PCode" => "",
"num" => 1,
"Price" => 2,
"children" => [
0 => [
"Code" => "1.1",
"PCode" => "1",
"num" => 1,
"Price" => 3,
],
1 => [
"Code" => "1.2",
"PCode" => "1",
"num" => 1,
"Price" => 4,
],
],
],
1 => [
"Code" => "2",
"PCode" => "",
"num" => 1,
"Price" => 2,
"children" => [
0 => [
"Code" => "2.1",
"PCode" => "2",
"num" => 1,
"Price" => 3,
],
1 => [
"Code" => "2.2",
"PCode" => "2",
"num" => 1,
"Price" => 3,
"children" => [
0 => [
"Code" => "2.2.1",
"PCode" => "2.2",
"num" => 1,
"Price" => 3,
],
],
],
],
],
];
这里计算节点的合价=单价*数量
function sum($data)
{
$aaa = 0;
foreach ($data as $item) {
if (isset($item['children']) && count($item['children']) > 0) {
$item['TotalPrice'] = sum($item['children']); //合计
} else {
$item['TotalPrice'] = $item['num'] * $item['Price']; //合价
}
$aaa += $item['TotalPrice'];
}
return $aaa;
}
对方法进行调用
$a = sum($tree);
print_r($a);//例子数据中的合计为13
边栏推荐
- 蚂蚁集团g6初探
- AWD learning
- Sqli - Labs Clearance (less6 - less14)
- JS divides an array into groups of three
- Common prototype methods of JS array
- SQLI-LABS通关(less18-less20)
- JS delete the last character of the string
- Latex warning: citation "*****" on page y undefined on input line*
- Kotlin - verify whether the time format is yyyy MM DD hh:mm:ss
- Log - 7 - record a major error in missing documents (A4 paper)
猜你喜欢
The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
PHP Session原理简析
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
In depth study of JVM bottom layer (IV): class file structure
Fe - wechat applet - Bluetooth ble development research and use
Sublime text configuring PHP compilation environment
随机推荐
js的防抖和节流
QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
Virtualenv and pipenv installation
SQL injection closure judgment
PXC high availability cluster summary
查询GPU时无进程运行,但是显存却被占用了
PHP Session原理简析
Log - 7 - record a major error in missing documents (A4 paper)
uniapp引入本地字体
In depth study of JVM bottom layer (3): garbage collector and memory allocation strategy
Pytest (1) case collection rules
Sqli-labs customs clearance (less15-less17)
sqli-labs通关汇总-page3
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
Usage of map and foreach in JS
pm2简单使用和守护进程
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
There are multiple good constructors and room will problem
Loops in tensorrt
Latex compilation error I found no \bibstyle &\bibdata &\citation command