


PHP tree-implementation method that does not require recursion, php tree does not require recursion_PHP tutorial
PHP tree - an implementation method that does not require recursion, php tree does not require recursion
PHP tree - an implementation method that does not require recursion
/** * 创建父节点树形数组 * 参数 * $ar 数组,邻接列表方式组织的数据 * $id 数组中作为主键的下标或关联键名 * $pid 数组中作为父键的下标或关联键名 * 返回 多维数组 **/ function find_parent($ar, $id='id', $pid='pid') { foreach($ar as $v) $t[$v[$id]] = $v; foreach ($t as $k => $item){ if( $item[$pid] ){ if( ! isset($t[$item[$pid]]['parent'][$item[$pid]]) ) $t[$item[$id]]['parent'][$item[$pid]] =& $t[$item[$pid]]; } } return $t; } /** * 创建子节点树形数组 * 参数 * $ar 数组,邻接列表方式组织的数据 * $id 数组中作为主键的下标或关联键名 * $pid 数组中作为父键的下标或关联键名 * 返回 多维数组 **/ function find_child($ar, $id='id', $pid='pid') { foreach($ar as $v) $t[$v[$id]] = $v; foreach ($t as $k => $item){ if( $item[$pid] ) { $t[$item[$pid]]['child'][$item[$id]] =& $t[$k]; } } return $t; } $data = array( array('ID'=>1, 'PARENT'=>0, 'NAME'=>'祖父'), array('ID'=>2, 'PARENT'=>1, 'NAME'=>'父亲'), array('ID'=>3, 'PARENT'=>1, 'NAME'=>'叔伯'), array('ID'=>4, 'PARENT'=>2, 'NAME'=>'自己'), array('ID'=>5, 'PARENT'=>4, 'NAME'=>'儿子'), ); $p = find_parent($data, 'ID', 'PARENT'); $c = find_child($data, 'ID', 'PARENT'); Print_r ($c);
Execution effect:
Array ( [1] => Array ( [ID] => 1 [PARENT] => 0 [NAME] => 祖父 [child] => Array ( [2] => Array ( [ID] => 2 [PARENT] => 1 [NAME] => 父亲 [child] => Array ( [4] => Array ( [ID] => 4 [PARENT] => 2 [NAME] => 自己 [child] => Array ( [5] => Array ( [ID] => 5 [PARENT] => 4 [NAME] => 儿子 ) ) ) ) ) [3] => Array ( [ID] => 3 [PARENT] => 1 [NAME] => 叔伯 ) ) ) [2] => Array ( [ID] => 2 [PARENT] => 1 [NAME] => 父亲 [child] => Array ( [4] => Array ( [ID] => 4 [PARENT] => 2 [NAME] => 自己 [child] => Array ( [5] => Array ( [ID] => 5 [PARENT] => 4 [NAME] => 儿子 ) ) ) ) ) [3] => Array ( [ID] => 3 [PARENT] => 1 [NAME] => 叔伯 ) [4] => Array ( [ID] => 4 [PARENT] => 2 [NAME] => 自己 [child] => Array ( [5] => Array ( [ID] => 5 [PARENT] => 4 [NAME] => 儿子 ) ) ) [5] => Array ( [ID] => 5 [PARENT] => 4 [NAME] => 儿子 ) )
The above PHP tree-implementation method that does not require recursion is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support Bangkejia.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
