当前位置:网站首页>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
边栏推荐
- Find in laravel8_ in_ Usage of set and upsert
- 【Torch】解决tensor参数有梯度,weight不更新的若干思路
- allennlp 中的TypeError: Object of type Tensor is not JSON serializable错误
- Error in running test pyspark in idea2020
- Use matlab to realize: chord cut method, dichotomy, CG method, find zero point and solve equation
- Alpha Beta Pruning in Adversarial Search
- [introduction to information retrieval] Chapter II vocabulary dictionary and inverted record table
- Cognitive science popularization of middle-aged people
- 中年人的认知科普
- Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
猜你喜欢
随机推荐
Classloader and parental delegation mechanism
【调参Tricks】WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach
使用Matlab实现:Jacobi、Gauss-Seidel迭代
Three principles of architecture design
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
parser.parse_args 布尔值类型将False解析为True
SSM supermarket order management system
【Torch】解决tensor参数有梯度,weight不更新的若干思路
Oracle EBS interface development - quick generation of JSON format data
SSM laboratory equipment management
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)
Determine whether the version number is continuous in PHP
【Torch】最简洁logging使用指南
[torch] the most concise logging User Guide
Drawing mechanism of view (3)
view的绘制机制(三)
Two table Association of pyspark in idea2020 (field names are the same)
Typeerror in allenlp: object of type tensor is not JSON serializable error
ARP attack
view的绘制机制(二)









