On the discussion of recursive algorithm

Source: Internet
Author: User
Younger brother is still not graduating students, now in the internship to do a project, and encountered a recursive problem, I hope you help!
The problem is simple, the classic recursive traversal array problem

 
  Array (' id ' = ' 1 ', ' name ' + ' role management ', ' pid ' = ' 0 ',), ' 1 ' =>array (' id ' = ' + ' 2 ', ' name ' = ' + ' Add character ', ' pid ' = ') 1 ',), ' 2 ' =>array (' id ' = ' 3 ', ' name ' = ' = ' role list ', ' pid ' = ' 2 ', '), ' 3 ' =>array (' id ' = ' 4 ', ' name ' = ' + ' menu Management ') , ' pid ' = ' 0 ', ' 4 ' =>array (' id ' = ' 5 ', ' name ' = ' "foreground management ', ' pid ' = ' 4 ', '), ' 5 ' =>array (' id ' = ' 6 ', ' Name ' = ' + ' add ', ' pid ' = ' 5 ', ' 6 ' =>array (' id ' = ' 7 ', ' name ' = ' modify ', ' pid ' = ' 5 ', '), ' 7 ' =>array (' id ' = > ' 8 ', ' name ' = ' list ', ' pid ' = ' 5 ', ' 8 ' =>array (' id ' = ' 9 ', ' name ' = ' ' admin ', ' pid ' = ' 4 ',), ' 9 ' = = Array (' id ' = ' + ', ' name ' = ' + ' add ', ' pid ' = ' + ' 9 ', '), ' =>array ' (' id ' = ' = ' ', ' name ' = ' ' modify ', ' pid ' = ' 9 ') ,), ' One ' =>array (' id ' = ' + ', ' name ' = = ' list ', ' pid ' = ' 9 ',),); >


Just like this, recursive traversal, there are two results to generate.
An array of standard hierarchical relationships
There is another one for the drop-down list,

In addition, I check the information, found that can also use pointers, students daoxing is still shallow, please many advice!!!


Reply to discussion (solution)

  The array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' a '), * 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = = ' First level column two '), * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '), * 4 = = Array (' id ' = ' 4 ', ' Parenti d ' =>1, ' name ' = ' Two-level column two '), * 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two-column three '), * 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three column A '), * 7 = = Array (' id ' = ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' three ' column two ')   ) *) */public function __construct ($arr =array ()) {$this->arr = $arr;   $this->ret = ";    Return Is_array ($arr);} /*** Gets the parent series group * @param int* @return array*/public function get_parent ($myid) {$newarr = array (); if (!isset ($this->arr[$ myID]) return false; $pid = $this->arr[$myid] [' parentid ']; $pid = $this->arr[$pid] [' ParentID '];if (Is_array ($ This->arr) {foreach ($this->arr as $id = + $a) {if ($a [' parentid '] = = $pid) $newarr [$id] = $a;}}    return $newarr;} /*** Get sub-series Group * @param int* @return arRay*/public function Get_child ($myid) {$a = $newarr = Array (), if (Is_array ($this->arr)) {foreach ($this->arr as $id = > $a) {if ($a [' parentid '] = = $myid) $newarr [$id] = $a;}} Return $newarr?    $newarr: false;} /*** gets the current position array * @param int* @return array*/public function Get_pos ($myid,& $newarr) {$a = array (); if (!isset ($this        arr[$myid])) return false; $newarr [] = $this->arr[$myid]; $pid = $this->arr[$myid] [' ParentID '];if (isset ($this->arr[$pid])) {$this Get_pos ($pid, $newarr);} if (Is_array ($newarr)) {Krsort ($newarr), foreach ($newarr as $v) {$a [$v [' id ']] = $v;}}    return $a;} /*** Get the tree structure * @param int ID, which means to get all the children under this ID * @param the Basic code of the string generation tree structure, for example: "\ $spacer \ $name  "* @param int is selected ID, such as in the Tree drop-down box need to use the * @return string*/public function Get_tree ($myid, $str, $sid = 0, $adds =", $str _gr OUP = ") {$number =1, $child = $this->get_child ($myid);p Rint_r ($child), if (Is_array ($child)) {$total = count ($child); foreach ($child as $id + $value) {$j = $k = ", if ($number = = $total) {$j. = $this->icon[2];///If the last child uses └}else{$j. = $ this->icon[1]; $k = $adds? $this->icon[0]: ';} $spacer = $adds? $adds. $j: "; ├ $selected = $id = = $sid? ' Selected ': '; Drop-down Select @extract ($value);//This method assigns the corresponding key value to the following character variable $nstr, $str _group$parentid = = 0 && $str _group? Eval ("\ $nstr = \" $str _group\ "): eval (" \ $nstr = \ "$str \"; "); $this->ret. = $nstr; $nbsp = $this->nbsp; $this->get_tree ($id, $str, $sid, $adds. $k. $nbsp, $str _group); If the subset is an array, recursively calls $number++;}} return $this->ret;}} $arr = Array (1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' World '), 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' China '), 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' Us '), 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>3, ' name ' = ' Hunan '), 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' Beijing '), 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>4, ' name ' = ' New York '), 7 = = Array (' id ' = = ' 7 ', ' ParentID ' =>5, ' name ' = ' Changsha '); $tree = new Tree ($arr); $str = "
 
 
      \ $spacer \ $name
"; Echo $tree->get_tree (0, $str);? >

  The array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' a '), * 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = = ' First level column two '), * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '), * 4 = = Array (' id ' = ' 4 ', ' Parenti d ' =>1, ' name ' = ' Two-level column two '), * 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two-column three '), * 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three column A '), * 7 = = Array (' id ' = ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' three ' column two ')   ) *) */public function __construct ($arr =array ()) {$this->arr = $arr;   $this->ret = ";    Return Is_array ($arr);} /*** Gets the parent series group * @param int* @return array*/public function get_parent ($myid) {$newarr = array (); if (!isset ($this->arr[$ myID]) return false; $pid = $this->arr[$myid] [' parentid ']; $pid = $this->arr[$pid] [' ParentID '];if (Is_array ($ This->arr) {foreach ($this->arr as $id = + $a) {if ($a [' parentid '] = = $pid) $newarr [$id] = $a;}}    return $newarr;} /*** Get sub-series Group * @param int* @return arRay*/public function Get_child ($myid) {$a = $newarr = Array (), if (Is_array ($this->arr)) {foreach ($this->arr as $id = > $a) {if ($a [' parentid '] = = $myid) $newarr [$id] = $a;}} Return $newarr?    $newarr: false;} /*** gets the current position array * @param int* @return array*/public function Get_pos ($myid,& $newarr) {$a = array (); if (!isset ($this        arr[$myid])) return false; $newarr [] = $this->arr[$myid]; $pid = $this->arr[$myid] [' ParentID '];if (isset ($this->arr[$pid])) {$this Get_pos ($pid, $newarr);} if (Is_array ($newarr)) {Krsort ($newarr), foreach ($newarr as $v) {$a [$v [' id ']] = $v;}}    return $a;} /*** Get the tree structure * @param int ID, which means to get all the children under this ID * @param the Basic code of the string generation tree structure, for example: "\ $spacer \ $name  "* @param int is selected ID, such as in the Tree drop-down box need to use the * @return string*/public function Get_tree ($myid, $str, $sid = 0, $adds =", $str _gr OUP = ") {$number =1, $child = $this->get_child ($myid);p Rint_r ($child), if (Is_array ($child)) {$total = count ($child); foreach ($child as $id + $value) {$j = $k = ", if ($number = = $total) {$j. = $this->icon[2];///If the last child uses └}else{$j. = $ this->icon[1]; $k = $adds? $this->icon[0]: ';} $spacer = $adds? $adds. $j: "; ├ $selected = $id = = $sid? ' Selected ': '; Drop-down Select @extract ($value);//This method assigns the corresponding key value to the following character variable $nstr, $str _group$parentid = = 0 && $str _group? Eval ("\ $nstr = \" $str _group\ "): eval (" \ $nstr = \ "$str \"; "); $this->ret. = $nstr; $nbsp = $this->nbsp; $this->get_tree ($id, $str, $sid, $adds. $k. $nbsp, $str _group); If the subset is an array, recursively calls $number++;}} return $this->ret;}} $arr = Array (1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' World '), 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' China '), 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' Us '), 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>3, ' name ' = ' Hunan '), 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' Beijing '), 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>4, ' name ' = ' New York '), 7 = = Array (' id ' = = ' 7 ', ' ParentID ' =>5, ' name ' = ' Changsha '); $tree = new Tree ($arr); $str = "
 
 
      \ $spacer \ $name
"; Echo $tree->get_tree (0, $str);? >


This returns a string, how can I return an array?

The essence area has too many related posts, suggest you first look
In general, only when they can not find or dissatisfied with the experience of others, they innovate

The essence area has too many related posts, suggest you first look
In general, only when they can not find or dissatisfied with the experience of others, they innovate

Well, thank you!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.