当前位置:网站首页>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 ']
];
}
边栏推荐
- Qt|qlabole change line spacing when displaying multiple lines
- Network solutions for Alibaba cloud services
- Raspberry Pie 3 connected to WiFi
- NVIDIA programmable reasoning accelerator tensorrt learning notes (II) - practical operation
- Hotel room management system based on jsp+servlet+mysql
- Advanced C language (11) - user defined data types
- Chapter 3 business function development (query clues)
- Nuscenes dataset 3D mot demo, end-to-end target detection and tracking, joint detection and tracking framework
- CM4 development cross compilation tool chain production
- The fifth day of MATLAB learning (cycle type)
猜你喜欢

A simple hotel background management system based on jsp+servlet+mysql

Source code of short video live broadcast system

Chapter 3 business function development (realize the real-time response of the select all button)

Raspberrypico serial communication

ArcGIS Pro脚本工具(10)——从图层生成.stylx样式符号

This week's big news | FCC exposed Pico 4 VR all-in-one machine, and leipeng's parent company established a smart glasses laboratory

Source code of pet adoption management system implemented by ssm+jsp+mysql

Ensembles in RNA counts data in TCGA_ ID to gene_ Method of ID

Teach you to understand the computer optometry sheet

Memcached data cache database (improve efficiency)
随机推荐
[5g NR] 3GPP common protocol sorting
Message Oriented Middleware
Does the server operation and maintenance need to be online 24 hours? Do you need to work overtime on weekends?
DIY can decorate the mall system, you can also have!
Data warehouse ODS, DWD floor, 220616, HM,
Advanced C language (XIII) - Example Analysis of dynamic memory management
Database query optimization
JS pop up picture Lightbox light box plug-in spotlight.js
阿里云服务的网络解决方案
[dark horse programmer] redis learning notes 003: redis transactions
Advanced C language (XII) - dynamic memory management
Talk about your transformation test development process
Basis 33: XPath acquisition methods of page elements under various browsers
Advanced C language (11) - user defined data types
Hotel room management system based on jsp+servlet+mysql
公寓报修系统(IDEA,SSM,MySQL)
Chapter 3 business function development (realize the real-time response of the select all button)
Bigdecimel in words
IP command usage details
第3章业务功能开发(实现全选按钮实时的响应)