function forTreeGetList($tab = " ", $y = 1, $parentId = null, $source, $levelBegin = 0, $levelEnd = 100, $path = "/") { $result = array(); $children = getChildren($parentId, $source); $childrenCount = count($children); foreach ($source as $item) { if ($item["parentId"] == $parentId) { $node = array(); $node["id"] = $item["id"]; $node["parentId"] = $item["parentId"]; $node["url"] = isset($item["url"]) ? $item["url"] : ""; $node["icon"] = isset($item["icon"]) ? $item["icon"] : ""; $node["logo"] = isset($item["logo"]) ? $item["logo"] : ""; $node["status"] = $item["status"]; $node["level"] = $levelBegin; $node["path"] = $path . "/" . $item["name"]; $node["title"] = $item["name"]; $treeChar = ""; $mySplitText = ""; if ($childrenCount > 1) { $treeChar = "├── "; // "┤──"; } else { $treeChar = "└── "; // "┘──"; } if ($y > 1) { $mySplitText = "│ "; } else { $mySplitText = " "; } $node["nodeTag"] = $tab . $mySplitText . $treeChar; if ($levelBegin < $levelEnd) { $child = forTreeGetList($tab . $mySplitText, $childrenCount, $item["id"], $source, $levelBegin + 1, $levelEnd, $node["path"]); array_push($result, $node); $result = array_merge($result, $child); } $childrenCount--; } } return $result; } function getChildren($parentId, $source) { $result = array(); foreach ($source as $item) { if ($item["parentId"] == $parentId) { array_push($result, $item); } } return $result; } function getParentNodes($source = array(), $id = 1, $idFieldName = "id", $parentIdFieldName = "parentId") { $parentNodes = array(); foreach ($source as $item) { if ($item[$idFieldName] == $id ) { array_push($parentNodes, $item ); $parentNode = getParentNodes($source, $item[$parentIdFieldName], $idFieldName, $parentIdFieldName); if ($parentNode) { $parentNodes = array_merge($parentNodes, $parentNode); } } } return $parentNodes ; // return array_reverse($parentNodes); }