Ask for unlimited classification of the current position how to write, top people have points!

Source: Internet
Author: User
Below is an infinite classification of the code, want to ask about the use of function Get_pos in the class, thank you!!!

  Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' = ' first column '), * 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' =&gt ;' First level column two '), * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two '), * 4 = = Array (' id ' = ' 4 ', '      ParentID ' =>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 one '), * 7 = = Array (' id ' = = ' 7 ', ' ParentID ' =>3, ' NA       Me ' = ' three-column two ') * */function tree ($arr =array ()) {$this->arr = $arr;       $this->ret = ";    Return Is_array ($arr); /** * Get Parent series Group * @param int * @return Array */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 */function Get_child ($myid) {$a = $newarr = Arr        Ay (); 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 */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 tree structure *-------------------------------------* @aut Hor yangyunzhou@foxmail.com * @param $myid means to get all the children under this ID * @param $str generate a tree-structured codebase, for example: "\ $spacer \ $name  "* @param $sid the selected ID, such as in the Tree drop-down box need to use * @param $adds * @param $str _group */function Get_tree ($myid,        $STR, $sid = 0, $adds = ", $str _group =") {$number = 1;        $child = $this->get_child ($myid);            if (Is_array ($child)) {$total = count ($child);                foreach ($child as $id + $a) {$j = $k = ';                if ($number = = $total) {$j. = $this->icon[2];                    } else {$j. = $this->icon[1]; $k = $adds?                $this->icon[0]: "; } $spacer = $adds?                $adds. $j: "; $selected = $id = = $sid?                ' Selected ': ';                @extract ($a); $parentid = = 0 && $str _group?                Eval ("\ $nstr = \" $str _group\ "): eval (" \ $nstr = \ "$str \"; ");                $this->ret. = $nstr;                $this->get_tree ($id, $str, $sid, $adds. $k. ", $str _group);            $number + +;      }  } return $this->ret;        }/** * Similar to the previous method, but allows multiple selection */function Get_tree_multi ($myid, $str, $sid = 0, $adds = ") {$number = 1;        $child = $this->get_child ($myid);            if (Is_array ($child)) {$total = count ($child);                foreach ($child as $id + $a) {$j = $k = ';                if ($number = = $total) {$j. = $this->icon[2];                    } else {$j. = $this->icon[1]; $k = $adds?                $this->icon[0]: "; } $spacer = $adds?                 $adds. $j: "; $selected = $this->have ($sid, $id)?                ' Selected ': ';                @extract ($a);                Eval ("\ $nstr = \" $str \ ";");                $this->ret. = $nstr;                $this->get_tree_multi ($id, $str, $sid, $adds. $k. ");            $number + +;   }} return $this->ret; The function has ($list, $item) {return (Strpos (',, '. $list. ', ', ', '. $item. ', ') '); }/** +------------------------------------------------* Formatted array +----------------------------------------- -------* @author yangyunzhou@foxmail.com +------------------------------------------------*/function get        Array ($myid =0, $sid =0, $adds = ") {$number = 1;        $child = $this->get_child ($myid);            if (Is_array ($child)) {$total = count ($child);                foreach ($child as $id + $a) {$j = $k = ';                if ($number = = $total) {$j. = $this->icon[2];                    } else {$j. = $this->icon[1]; $k = $adds?                $this->icon[0]: "; } $spacer = $adds?                $adds. $j: ";                @extract ($a); $a [' name '] = $spacer. '                '. $a [' name '];                $this->ret[$a [' id ']] = $a;  $FD = $adds. $k. ";              $this->getarray ($id, $sid, $FD);            $number + +;    }} return $this->ret; }}?>


Reply to discussion (solution)

$arr = Array (1 = = Array (          ' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' = ' first column one '),          2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' A ' column two '),          3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column One '),          4 = > Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' two ' column two '),          5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' Name ' = = ' two column three '),          6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three '),          7 = = Array (' id ' = > ' 7 ', ' ParentID ' =>3, ' name ' = ' three-level column II ')          ; $tree = new Tree ($arr); $tree->get_pos ($myid, $newarr);

$arr = Array (1 = = Array (          ' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' = ' first column one '),          2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' A ' column two '),          3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column One '),          4 = > Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' two ' column two '),          5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' Name ' = = ' two column three '),          6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three '),          7 = = Array (' id ' = > ' 7 ', ' ParentID ' =>3, ' name ' = ' three-level column II ')          ; $tree = new Tree ($arr); $tree->get_pos ($myid, $newarr);



This I know, $tree->get_pos ($myid, $newarr), what is the value of $newarr?

An array of your own definition

An array of your own definition

Now I just want to know what the value of this array is.


An array of your own definition

Now I just want to know what the value of this array is.

$arr = Array (1 = = Array (          ' id ' = = ' 1 '),          2 = = Array (' id ' = = ' 2 '),          3 = = Array (' id ' = = ' 3 '),          4 = = Array (' id ' = ' 4 '),          5 = = Array (' id ' = = ' 5 '),          6 = = Array (' id ' = = ' 6 '),          7 = = Array (' id ' = > ' 7 ')          ); $tree = new Tree ($arr); $pos = $tree->get_pos (' 3 ', $arr); Var_dump ($pos);



The function is to place an array somewhere in the first place and then reverse it.

function Get_pos ($myid,& $newarr)
Should write
function Get_pos ($myid,& $newarr =array ())

That is, you do not need an explicit pass-through array
$newarr is just the working parameters at the time of recursion

Get_pos's role is to produce "marshmallow" data
That is, the path from the root node to the current node
For printing
CSDN > CSDN Forum > PHP > BASIC programming
This kind of content

  • 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.