当前位置:网站首页>PHP gets all child nodes under any parent node of the tree structure
PHP gets all child nodes under any parent node of the tree structure
2022-07-25 08:34:00 【Summer is already slightly cool】
One 、 explain
- Known data table has fields :
id, pid, nameEtc , To any parent node pid, Need to find out : All child nodes under the parent node - If the data exists in the data table , Through here
queryAll Methodsimulationsql Inquire aboutThe way to achieve
Two 、 Code
public function test()
{
$pid = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : 1;
$subIds = [];
$self = new self();
self::getSubIds($pid, $self, $subIds);
var_export($subIds);
die;
}
public function getSubIds($pid, self $dao, &$subIds)
{
$param = [];
if (is_array($pid)) {
$param['in'] = $pid; // The superior pid IN Inquire about
} else {
$param['eq'] = $pid; // The superior pid = Inquire about
}
// According to the parent node , Find child nodes of lower level
$subList = $dao->searchAll($param);
if (empty($subList)) {
return $subIds;
}
$tempSubIds = array_column($subList, 'id'); // Get all the subordinates of this node id
$subIds = array_merge($subIds, $tempSubIds); // User subordinate ID Merge
$this->getSubIds($tempSubIds, $dao, $subIds);
}
// Simulate all operations of data table query
public function searchAll($param)
{
$result = [];
$data = self::getData();
if (array_key_exists('eq', $param)) {
// Equal to operation
$searchValue = $param['eq'];
foreach ($data as $value) {
if ($searchValue == $value['pid']) {
$result[] = $value;
}
}
}
if (array_key_exists('in', $param)) {
//in Inquire about
$searchArray = $param['in'];
foreach ($data as $value) {
if (in_array($value['pid'], $searchArray)) {
$result[] = $value;
}
}
}
return $result;
}
// Simulate the data stored in the data table
public function getData()
{
return [
['id' => 1, 'pid' => 0, 'name' => ' mobile phone '],
['id' => 2, 'pid' => 1, 'name' => 'A mobile phone '],
['id' => 3, 'pid' => 2, 'name' => 'A Mobile phone charger '],
['id' => 4, 'pid' => 2, 'name' => 'A Mobile phone earphone '],
['id' => 5, 'pid' => 1, 'name' => 'B mobile phone '],
['id' => 6, 'pid' => 5, 'name' => 'B Mobile phone charger '],
['id' => 7, 'pid' => 5, 'name' => 'B Mobile phone earphone '],
['id' => 8, 'pid' => 7, 'name' => 'B Mobile phone headset left headset '],
['id' => 9, 'pid' => 7, 'name' => 'B Mobile phone headset right headset '],
['id' => 10, 'pid' => 0, 'name' => ' The computer ']
];
}
边栏推荐
- 本周大新闻|FCC曝光Pico 4 VR一体机,雷朋母公司建立智能眼镜实验室
- TCGA simple download tool V16 installation error
- Swift初始化器及可选链
- 【5G NR】3GPP常用协议整理
- Redis4.0.14 sentinel automatic failover failed
- 哈希表刷题(上)
- CM4 development cross compilation tool chain production
- [dark horse programmer] redis learning notes 004: master-slave replication + sentinel mode + cluster
- Huawei device remote login (Telnet, SSH) configuration
- Apartment repair reporting system (idea, SSM, MySQL)
猜你喜欢

第3章业务功能开发(查询线索)

Record the process of two multi terminal troubleshooting

Rstudio shows that it can't connect to the web page, or it has a new website.

阿里云服务的网络解决方案

本周大新闻|FCC曝光Pico 4 VR一体机,雷朋母公司建立智能眼镜实验室

【黑马程序员】Redis学习笔记004:主从复制+哨兵模式+集群

A simple SQL injection shooting range exercise

How to set up a personal website for free

Keep your Eyes on the Lane: Real-time Attention-guided Lane Detection

ArcGIS Pro scripting tool (10) -- generate.Stylx style symbols from layers
随机推荐
DIY can decorate the mall system, you can also have!
Machine learning theory and case analysis (Part2) -- Regression
Centernet network structure construction
Initial knowledge of WebService (generate jar packages and call methods in remote services)
C # introductory series (30) -- exception handling
【黑马程序员】Redis学习笔记001:Redis简介+五种基本数据类型
【黑马程序员】Redis学习笔记002:持久化:RDB 和 AOF
孙子兵法随感
【黑马程序员】Redis学习笔记005:企业级解决方案
Sun Tzu's art of war
Advanced C language (11) - user defined data types
Raspberry pie 4B parsing PWM
A simple hotel background management system based on jsp+servlet+mysql
Svg creative underline style JS special effect
[untitled]
聊下自己转型测试开发的历程
[dark horse programmer] redis learning notes 001: introduction to redis + five basic data types
JS pop up picture Lightbox light box plug-in spotlight.js
刷题《剑指Offer》day02
Test the mock data method of knowing and knowing