Created
February 18, 2014 04:24
-
-
Save gubi/9064631 to your computer and use it in GitHub Desktop.
Explode tree function (taken from http://kvz.io/blog/2007/10/03/convert-anything-to-tree-structures-in-php/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function explodeTree($array, $delimiter = "_", $baseval = false) { | |
| if(!is_array($array)) return false; | |
| $splitRE = "/" . preg_quote($delimiter, "/") . "/"; | |
| $returnArr = array(); | |
| foreach ($array as $key => $val) { | |
| // Get parent parts and the current leaf | |
| $parts = preg_split($splitRE, $key, -1, PREG_SPLIT_NO_EMPTY); | |
| $leafPart = array_pop($parts); | |
| // Build parent structure | |
| // Might be slow for really deep and large structures | |
| $parentArr = &$returnArr; | |
| foreach ($parts as $part) { | |
| if (!isset($parentArr[$part])) { | |
| $parentArr[$part] = array(); | |
| } elseif (!is_array($parentArr[$part])) { | |
| if ($baseval) { | |
| $parentArr[$part] = array("__base_val" => $parentArr[$part]); | |
| } else { | |
| $parentArr[$part] = array(); | |
| } | |
| } | |
| $parentArr = &$parentArr[$part]; | |
| } | |
| // Add the final part to the structure | |
| if (empty($parentArr[$leafPart])) { | |
| $parentArr[$leafPart] = $val; | |
| } elseif ($baseval && is_array($parentArr[$leafPart])) { | |
| $parentArr[$leafPart]["__base_val"] = $val; | |
| } | |
| } | |
| return $returnArr; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function plotTree($arr, $indent=0, $mother_run=true){ | |
| if ($mother_run) { | |
| // the beginning of plotTree. We're at rootlevel | |
| print "start<br />"; | |
| } | |
| foreach ($arr as $k=>$v){ | |
| // skip the baseval thingy. Not a real node. | |
| if ($k == "__base_val") continue; | |
| // determine the real value of this node. | |
| $show_val = (is_array($v) ? $v["__base_val"] : $v); | |
| // show the indents | |
| print str_repeat(" ", $indent); | |
| if ($indent == 0) { | |
| // this is a root node. no parents | |
| print "O "; | |
| } elseif (is_array($v)){ | |
| // this is a normal node. parents and children | |
| print "+ "; | |
| } else { | |
| // this is a leaf node. no children | |
| print "- "; | |
| } | |
| // show the actual node | |
| print $k . " (" . $show_val. ")" . "<br />"; | |
| if (is_array($v)) { | |
| // this is what makes it recursive, rerun for childs | |
| plotTree($v, ($indent+1), false); | |
| } | |
| } | |
| if ($mother_run) { | |
| print "end<br />"; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment