当前位置:网站首页>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
边栏推荐
- Delete the contents under the specified folder in PHP
- PointNet理解(PointNet实现第4步)
- Determine whether the version number is continuous in PHP
- @Transational踩坑
- Principle analysis of spark
- Oracle EBS ADI development steps
- MySQL has no collation factor of order by
- spark sql任务性能优化(基础)
- DNS attack details
- Oracle 11g uses ords+pljson to implement JSON_ Table effect
猜你喜欢

Error in running test pyspark in idea2020

点云数据理解(PointNet实现第3步)
![[model distillation] tinybert: distilling Bert for natural language understanding](/img/c1/e1c1a3cf039c4df1b59ef4b4afbcb2.png)
[model distillation] tinybert: distilling Bert for natural language understanding

SSM garbage classification management system
![[medical] participants to medical ontologies: Content Selection for Clinical Abstract Summarization](/img/24/09ae6baee12edaea806962fc5b9a1e.png)
[medical] participants to medical ontologies: Content Selection for Clinical Abstract Summarization

Point cloud data understanding (step 3 of pointnet Implementation)

Practice and thinking of offline data warehouse and Bi development

Play online games with mame32k

Proteus -- RS-232 dual computer communication

TCP attack
随机推荐
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
SSM实验室设备管理
view的绘制机制(一)
离线数仓和bi开发的实践和思考
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
Huawei machine test questions-20190417
A slide with two tables will help you quickly understand the target detection
Drawing mechanism of view (II)
ssm垃圾分类管理系统
CONDA creates, replicates, and shares virtual environments
Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
The first quickapp demo
Oracle apex Ajax process + dy verification
[Bert, gpt+kg research] collection of papers on the integration of Pretrain model with knowledge
allennlp 中的TypeError: Object of type Tensor is not JSON serializable错误
【调参Tricks】WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach
parser. parse_ Args boolean type resolves false to true
Check log4j problems using stain analysis
Using compose to realize visible scrollbar
Interpretation of ernie1.0 and ernie2.0 papers