当前位置:网站首页>Calculate the total in the tree structure data in PHP
Calculate the total in the tree structure data in PHP
2022-07-02 07:28:00 【Snow wind in the night sky】
Preface
In the process of project development, sometimes we encounter tree structure data calculation, which calculates the sum of all data values from the last level node to the root node , That is, calculate that the sum of the child node data is equal to the parent node , And so on to the root node .
The following method uses the recursive query calculation method .
Tree structure data example :
$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,
],
],
],
],
],
];
The total price of the node is calculated here = The unit price * Number
function sum($data)
{
$aaa = 0;
foreach ($data as $item) {
if (isset($item['children']) && count($item['children']) > 0) {
$item['TotalPrice'] = sum($item['children']); // total
} else {
$item['TotalPrice'] = $item['num'] * $item['Price']; // Total price
}
$aaa += $item['TotalPrice'];
}
return $aaa;
}
Call the method
$a = sum($tree);
print_r($a);// The total in the example data is 13
边栏推荐
- 矩阵的Jordan分解实例
- Implement interface Iterable & lt; T>
- Optimization method: meaning of common mathematical symbols
- Data warehouse model fact table model design
- parser.parse_args 布尔值类型将False解析为True
- Oracle APEX 21.2 installation et déploiement en une seule touche
- 自然辩证辨析题整理
- 【Torch】解决tensor参数有梯度,weight不更新的若干思路
- Agile development of software development pattern (scrum)
- SSM二手交易网站
猜你喜欢
[introduction to information retrieval] Chapter 1 Boolean retrieval
[paper introduction] r-drop: regulated dropout for neural networks
【模型蒸馏】TinyBERT: Distilling BERT for Natural Language Understanding
叮咚,Redis OM对象映射框架来了
view的绘制机制(一)
【BERT,GPT+KG调研】Pretrain model融合knowledge的论文集锦
SSM supermarket order management system
Oracle EBs and apex integrated login and principle analysis
sparksql数据倾斜那些事儿
【信息检索导论】第六章 词项权重及向量空间模型
随机推荐
Ding Dong, here comes the redis om object mapping framework
CRP implementation methodology
Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
離線數倉和bi開發的實踐和思考
JSP intelligent community property management system
Yolov5 practice: teach object detection by hand
Illustration of etcd access in kubernetes
ORACLE EBS ADI 开发步骤
view的绘制机制(二)
【信息检索导论】第二章 词项词典与倒排记录表
Explain in detail the process of realizing Chinese text classification by CNN
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
CONDA creates, replicates, and shares virtual environments
Play online games with mame32k
sparksql数据倾斜那些事儿
Oracle 11.2.0.3 handles the problem of continuous growth of sysaux table space without downtime
架构设计三原则
Proteus -- RS-232 dual computer communication
Determine whether the version number is continuous in PHP
Transform the tree structure into array in PHP (flatten the tree structure and keep the sorting of upper and lower levels)