Get the path relationship of infinite categories of left and right values
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 08:50:57
Original
976 people have browsed it
Recently, I need something that can display something like 1 > 2 1 > 2 > 3 1 > 2 > 3 > 4 . I don’t know the algorithm, so I just took a lot of detours. Of course, I hope you have a better way. Tucao is welcome.
- public function getPaths($nodes)
- {
- $levels = $paths = array();
- // Reorganize array levels
- foreach ($nodes as $id => $node)
- {
- $levels[ $node['level']][$id] = $node;
- }
- for ($i = 0; $i < count($levels); $i++)
- {
- if (is_array($levels[$ i]))
- {
- foreach ($levels[$i] as $level)
- {
- $id = $level['id'];
- $parent_id = $level['parent_id'];
- $parent = $ paths[$parent_id];
- // There is a parent and appended to the current node
- if ($parent)
- {
- $paths[$id] = $parent;
- $paths[$id][] = $level[ 'id'];
- }
- else
- {
- $paths[$id][] = $level['id'];
- }
- }
- }
- }
- return $paths;
- }
Copy code
|
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
Latest Articles by Author
-
2025-02-26 03:58:14
-
2025-02-26 03:38:10
-
2025-02-26 03:17:10
-
2025-02-26 02:49:09
-
2025-02-26 01:08:13
-
2025-02-26 00:46:10
-
2025-02-25 23:42:08
-
2025-02-25 22:50:13
-
2025-02-25 21:54:11
-
2025-02-25 20:45:11