Home Backend Development PHP Tutorial The class for formatting the node tree is not well written and has no algorithm, but it can be used.

The class for formatting the node tree is not well written and has no algorithm, but it can be used.

Jul 25, 2016 am 09:09 AM

Format the classified data fetched from the database, such as:

News
--Sports News
--Entertainment News
Finance
--Forex
--Finance
  1. <?php
  2. class tree
  3. {
  4. /**Raw data*/
  5. public $original;
  6. /**key name of id*/
  7. public $id;
  8. /**父The key name of the parent id*/
  9. public $parentId;
  10. /**id during initialization*/
  11. protected $initId;
  12. /**node level*/
  13. protected $thisLevel = 0;
  14. /**final tree*/
  15. protected $tree = array();
  16. /**
  17. * 构造函数
  18. +------------------------------------------
  19. * @access public
  20. +------------------------------------------
  21. * @param array $original
  22. * Constructor
  23. +------------------------------------------------
  24. * @access public
  25. +------------------------------------------------
  26. * @ param array $original original data
  27. * @param string $id key name of id
  28. * @param string $parentId key name of parent id
  29. +------------------- -----------------------
  30. * @return void
  31. * @param string $id
  32. * Initialization
  33. +------------------------------------------------
  34. * @ access public
  35. +------------------------------------------------
  36. * @param array $original Original data
  37. * @param string $id The key name of id
  38. * @param string $parentId The key name of parent id
  39. +-------------------------- -----------------------
  40. * @return void
  41. * @param string $parentId 父
  42. * Get initial node
  43. +------------------------------------------------ ---
  44. * @access protected
  45. +---------------------------------------- ------
  46. * @param int $parentId The level of the initial node
  47. +---------------------------------- ---------------
  48. * @return array $parentTree
  49. +------------------------------------------
  50. * @return void
  51. */
  52. public function __construct($original='',$id='',$parentId='')
  53. {
  54. if($original && $id && $parentId)
  55. {
  56. $this->initialize($original,$id,$parentId);
  57. }
  58. }
  59. /**
  60. * 初始化
  61. +------------------------------------------
  62. * @access public
  63. +------------------------------------------
  64. * @param array $original
  65. * Get subtree
  66. +------------------------------------------------ ---
  67. * @access protected
  68. +---------------------------------------- ------
  69. * @param int $id node id
  70. * @param string $levelTag indentation mark
  71. +---------------------- --------------------------
  72. * @return void
  73. * @param string $id
  74. * Get the level of the node
  75. +--------------------------------------------- -------
  76. * @access protected
  77. +------------------------------------------------ -------------
  78. * @param int $parentId The parent id of the node
  79. +------------------------ ------------------------
  80. * @return void
  81. * @param string $parentId 父
  82. * Get the complete tree
  83. +--------------------------------------------- -------
  84. * @access public
  85. +------------------------------------------------ -------------
  86. * @param int $level From what level to get
  87. * @param string $levelTag Indent tag
  88. +------------- ------------------------------------
  89. * @return array $this->tree complete tree
  90. +------------------------------------------
  91. * @return void
  92. */
  93. public function initialize($original,$id,$parentId)
  94. {
  95. $this->original = $original;
  96. $this->id = $id;
  97. $this->parentId = $parentId;
  98. }
  99. /**
  100. * 获取初始节点
  101. +----------------------------------------------
  102. * @access protected
  103. +----------------------------------------------
  104. * @param int $parentId 初始&
  105. +----------------------------------------------
  106. * @return array $parentTree
  107. */
  108. protected function getParentTree($parentId)
  109. {
  110. $parentTree = array();
  111. foreach($this->original as $key=>$value)
  112. {
  113. if($value[$this->parentId] == $parentId)
  114. {
  115. array_push($parentTree,$value);
  116. }
  117. }
  118. return $parentTree;
  119. }
  120. /**&*/
  121. protected function getChildrenTree($id,$levelTag)
  122. {
  123. foreach($this->original as $key=>$value)
  124. {
  125. if($id == $value[$this->parentId])
  126. {
  127. if($levelTag)
  128. {
  129. $this->getLevel($value[$this->parentId]);
  130. $value['levelTag'] = str_repeat($levelTag,$this->thisLevel);
  131. $this->thisLevel = 0;
  132. }
  133. $this->tree[] = $value;
  134. $this->getChildrenTree($value[$this->id],$levelTag);
  135. }
  136. }
  137. }
  138. /**
  139. * 获取&🎜 +-------------------------------------------------🎜 * @access protected🎜 +-------------------------------------------------🎜 * @param int $parentId 节点的父id🎜 +-------------------------------------------------🎜 * @return void🎜 */🎜 protected function getLevel($parentId)🎜 {🎜 foreach($this->original as $key=>$value)🎜 {🎜 if($parentId == $value[$this->id] && $parentId != $this->initId)🎜 {🎜 $this->thisLevel++;🎜 $this->getLevel($value[$this->parentId]);🎜 }🎜 }🎜 }🎜🎜🎜 /**&*/🎜 public function getTree($parentId=0,$levelTag='')🎜 {🎜 $this->initId = $parentId;🎜 $parentTree = $this->getParentTree($parentId);🎜🎜 foreach($parentTree as $key=>$value)🎜 {🎜 $this->tree[] = $value;🎜 $this->getChildrenTree($value[$this->id],$levelTag);🎜 }🎜🎜 return $this->tree;🎜 }🎜🎜}
  140. $conf = array(
  141. 1 => array('id'=>'1','parentid'=>0,'name'=>'1'),
  142. 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
  143. 3 => array('id'=>'3','parentid'=>1,'name'=>'1-1'),
  144. 4 => array('id'=>'4','parentid'=>1,'name'=>'1-2'),
  145. 5 => array('id'=>'5','parentid'=>2,'name'=>'2-1'),
  146. 6 => array('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
  147. 7 => array('id'=>'7','parentid'=>4,'name'=>'1-2-1'),
  148. 8 => array('id'=>'8','parentid'=>5,'name'=>'2-1-1'),
  149. 9 => array('id'=>'9','parentid'=>8,'name'=>'2-1-1-1')
  150. );
  151. $tree = new tree($conf,'id','parentid');
  152. $arr = $tree->getTree(0,'   ');
  153. foreach($arr as $val)
  154. {
  155. if($val['levelTag'])
  156. {
  157. echo $val['levelTag'].'|- ';
  158. }
  159. echo $val['name'].'<br />';
  160. }
  161. ?>
复制代码
  1. <?php
  2. class tree
  3. {
  4. /**Raw data*/
  5. public $original;
  6. /**key name of id*/
  7. public $id;
  8. /**父The key name of the parent id*/
  9. public $parentId;
  10. /**id during initialization*/
  11. protected $initId;
  12. /**node level*/
  13. protected $thisLevel = 0;
  14. /**final tree*/
  15. protected $tree = array();
  16. /**
  17. * 构造函数
  18. +------------------------------------------
  19. * @access public
  20. +------------------------------------------
  21. * @param array $original
  22. * Constructor
  23. +------------------------------------------------
  24. * @access public
  25. +------------------------------------------------
  26. * @ param array $original original data
  27. * @param string $id key name of id
  28. * @param string $parentId key name of parent id
  29. +------------------- -----------------------
  30. * @return void
  31. * @param string $id
  32. * Initialization
  33. +------------------------------------------------
  34. * @ access public
  35. +------------------------------------------------
  36. * @param array $original Original data
  37. * @param string $id The key name of id
  38. * @param string $parentId The key name of parent id
  39. +-------------------------- -----------------------
  40. * @return void
  41. * @param string $parentId 父
  42. * Get initial node
  43. +------------------------------------------------ ---
  44. * @access protected
  45. +---------------------------------------- ------
  46. * @param int $parentId The level of the initial node
  47. +---------------------------------- ---------------
  48. * @return array $parentTree
  49. +------------------------------------------
  50. * @return void
  51. */
  52. public function __construct($original='',$id='',$parentId='')
  53. {
  54. if($original && $id && $parentId)
  55. {
  56. $this->initialize($original,$id,$parentId);
  57. }
  58. }
  59. /**
  60. * 初始化
  61. +------------------------------------------
  62. * @access public
  63. +------------------------------------------
  64. * @param array $original
  65. * Get subtree
  66. +------------------------------------------------ ---
  67. * @access protected
  68. +---------------------------------------- ------
  69. * @param int $id node id
  70. * @param string $levelTag indentation mark
  71. +---------------------- --------------------------
  72. * @return void
  73. * @param string $id
  74. * Get the level of the node
  75. +--------------------------------------------- -------
  76. * @access protected
  77. +------------------------------------------------ -------------
  78. * @param int $parentId The parent id of the node
  79. +------------------------ ------------------------
  80. * @return void
  81. * @param string $parentId 父
  82. * Get the complete tree
  83. +--------------------------------------------- -------
  84. * @access public
  85. +------------------------------------------------ -------------
  86. * @param int $level From what level to get
  87. * @param string $levelTag Indent tag
  88. +------------- ------------------------------------
  89. * @return array $this->tree complete tree
  90. +------------------------------------------
  91. * @return void
  92. */
  93. public function initialize($original,$id,$parentId)
  94. {
  95. $this->original = $original;
  96. $this->id = $id;
  97. $this->parentId = $parentId;
  98. }
  99. /**
  100. * 获取初始节点
  101. +----------------------------------------------
  102. * @access protected
  103. +----------------------------------------------
  104. * @param int $parentId 初始&
  105. +----------------------------------------------
  106. * @return array $parentTree
  107. */
  108. protected function getParentTree($parentId)
  109. {
  110. $parentTree = array();
  111. foreach($this->original as $key=>$value)
  112. {
  113. if($value[$this->parentId] == $parentId)
  114. {
  115. array_push($parentTree,$value);
  116. }
  117. }
  118. return $parentTree;
  119. }
  120. /**&*/
  121. protected function getChildrenTree($id,$levelTag)
  122. {
  123. foreach($this->original as $key=>$value)
  124. {
  125. if($id == $value[$this->parentId])
  126. {
  127. if($levelTag)
  128. {
  129. $this->getLevel($value[$this->parentId]);
  130. $value['levelTag'] = str_repeat($levelTag,$this->thisLevel);
  131. $this->thisLevel = 0;
  132. }
  133. $this->tree[] = $value;
  134. $this->getChildrenTree($value[$this->id],$levelTag);
  135. }
  136. }
  137. }
  138. /**
  139. * 获取&🎜 +-------------------------------------------------🎜 * @access protected🎜 +-------------------------------------------------🎜 * @param int $parentId 节点的父id🎜 +-------------------------------------------------🎜 * @return void🎜 */🎜 protected function getLevel($parentId)🎜 {🎜 foreach($this->original as $key=>$value)🎜 {🎜 if($parentId == $value[$this->id] && $parentId != $this->initId)🎜 {🎜 $this->thisLevel++;🎜 $this->getLevel($value[$this->parentId]);🎜 }🎜 }🎜 }🎜🎜🎜 /**&*/🎜 public function getTree($parentId=0,$levelTag='')🎜 {🎜 $this->initId = $parentId;🎜 $parentTree = $this->getParentTree($parentId);🎜🎜 foreach($parentTree as $key=>$value)🎜 {🎜 $this->tree[] = $value;🎜 $this->getChildrenTree($value[$this->id],$levelTag);🎜 }🎜🎜 return $this->tree;🎜 }🎜🎜}
  140. $conf = array(
  141. 1 => array('id'=>'1','parentid'=>0,'name'=>'1'),
  142. 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
  143. 3 => array('id'=>'3','parentid'=>1,'name'=>'1-1'),
  144. 4 => array('id'=>'4','parentid'=>1,'name'=>'1-2'),
  145. 5 => array('id'=>'5','parentid'=>2,'name'=>'2-1'),
  146. 6 => array('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
  147. 7 => array('id'=>'7','parentid'=>4,'name'=>'1-2-1'),
  148. 8 => array('id'=>'8','parentid'=>5,'name'=>'2-1-1'),
  149. 9 => array('id'=>'9','parentid'=>8,'name'=>'2-1-1-1')
  150. );
  151. $tree = new tree($conf,'id','parentid');
  152. $arr = $tree->getTree(0,'   ');
  153. foreach($arr as $val)
  154. {
  155. if($val['levelTag'])
  156. {
  157. echo $val['levelTag'].'|- ';
  158. }
  159. echo $val['name'].'<br />';
  160. }
  161. ?>
复制代码


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles