当前位置:网站首页>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
边栏推荐
- 【调参Tricks】WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach
- Drawing mechanism of view (I)
- Typeerror in allenlp: object of type tensor is not JSON serializable error
- spark sql任务性能优化(基础)
- 離線數倉和bi開發的實踐和思考
- ssm人事管理系统
- Oracle apex 21.2 installation and one click deployment
- Explanation of suffix of Oracle EBS standard table
- Transform the tree structure into array in PHP (flatten the tree structure and keep the sorting of upper and lower levels)
- Alpha Beta Pruning in Adversarial Search
猜你喜欢
【论文介绍】R-Drop: Regularized Dropout for Neural Networks
PointNet原理证明与理解
Agile development of software development pattern (scrum)
Oracle 11g uses ords+pljson to implement JSON_ Table effect
Implementation of purchase, sales and inventory system with ssm+mysql
Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
[introduction to information retrieval] Chapter II vocabulary dictionary and inverted record table
第一个快应用(quickapp)demo
Ding Dong, here comes the redis om object mapping framework
【信息检索导论】第七章搜索系统中的评分计算
随机推荐
Explain in detail the process of realizing Chinese text classification by CNN
ORACLE EBS 和 APEX 集成登录及原理分析
ssm+mysql实现进销存系统
CONDA creates, replicates, and shares virtual environments
ERNIE1.0 与 ERNIE2.0 论文解读
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
实现接口 Interface Iterable<T>
Oracle segment advisor, how to deal with row link row migration, reduce high water level
[introduction to information retrieval] Chapter II vocabulary dictionary and inverted record table
使用Matlab实现:幂法、反幂法(原点位移)
Module not found: Error: Can't resolve './$$_ gendir/app/app. module. ngfactory'
Oracle 11g uses ords+pljson to implement JSON_ Table effect
Play online games with mame32k
JSP intelligent community property management system
【模型蒸馏】TinyBERT: Distilling BERT for Natural Language Understanding
parser. parse_ Args boolean type resolves false to true
@Transational踩坑
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
Point cloud data understanding (step 3 of pointnet Implementation)
叮咚,Redis OM对象映射框架来了