PHP Infinite Pole Classification implementation (without recursion), PHP recursive _php tutorial

Source: Internet
Author: User
Tags php language

PHP Infinite Pole Classification implementation (without recursion), PHP recursion


Infinite pole classification is often used in development, for example: Department structure, article classification. The difficulty with infinite pole classification is "output" and "query", for example

  • Export the article category to
        list form;
      • Find the articles that are included in all categories under Category A.

    1. Principle of implementation

    This article introduces several common methods of realizing the idea of infinite class classification, and has advantages and disadvantages. "Improved pre-sequence traversal tree" data structure, easy to output and query, but in the mobile classification and general understanding is somewhat complex.

    2. Data structure

    ID FID title
    1 0 China
    2 1 Jiangsu
    3 1 Anhui
    4 8 Jiangyin
    5 3 Wuhu
    6 3 Hefei
    7 3 Bengbu
    8 2 Wuxi
    
      
         1, 'fid'=>0, 'title' => '中国'),         array('id'=>2, 'fid'=>1, 'title' => '江苏'),        array('id'=>3, 'fid'=>1, 'title' => '安徽'),        array('id'=>4, 'fid'=>8, 'title' => '江阴'),        array('id'=>5, 'fid'=>3, 'title' => '芜湖'),        array('id'=>6, 'fid'=>3, 'title' => '合肥'),        array('id'=>7, 'fid'=>3, 'title' => '蚌埠'),        array('id'=>8, 'fid'=>8, 'title' => '无锡')    );?>

    A classification tree is formed by the level "concatenation" between the categories through the parent class ID (FID). It is worth noting in tandem that the FID of categorical a cannot be the ID of its subclass.

    In the use of this data structure to output the most commonly used algorithm is "recursive", familiar with the PHP language friends must know that PHP is not good at recursion, and the number of recursion is limited (about 100 times, due to operating system and configuration vary).

    Since all recursion can be implemented using loops, a function of "infinite Class" classification is written according to PHP language features, which is more efficient than recursive implementations.

    3. Output UL List form

    Export the above data to the following HTML

    
      
         
         
    • 江苏
      • 无锡
        • 江阴
    • 安徽
      • 芜湖
      • 合肥
      • 蚌埠

    This HTML structure is handy for front-end use (using JavaScript and CSS to construct collapsible trees). The specific implementation procedure is as follows:

    
      
         
         

    4. Output Option List form

    江苏    无锡        江阴安徽    芜湖    合肥    蚌埠

    The specific implementation procedure is as follows:

    
         ' . Str_repeat ("", $op [' depth '] * 4). $op [' title '].    ' <;/option> '; }?><;/select>5. Find all subclasses of a class
         6. Find all the parent classes for a category
         7. Related functions
         =0;                $i-) {$node = $data [$i];                    if ($node [' fid '] = = $fid) {array_splice ($data, $i, 1);                    $result [] = $node [' id '];                    $cids [] = $node [' id '];                $flag = true;    }}} $fids = $cids;    } while ($flag = = = True); return $result;}    function Get_tree_parent ($data, $id) {$result = array ();    $obj = Array ();    foreach ($data as $node) {$obj [$node [' id ']] = $node; } $value = Isset ($obj [$id])?        $obj [$id]: null;        while ($value) {$id = null;                foreach ($data as $node) {if ($node [' id '] = = $value [' FID ']) {$id = $node [' id '];                $result [] = $node [' id '];            Break        }} if ($id = = = null) {$result [] = $value [' FID ']; } $value = Isset ($obj [$id])?    $obj [$id]: null;    } unset ($obj); return $result;} function Get_tree_ul ($data, $fID) {$stack = array ($FID);    $child = Array ();    $added _left = Array ();    $added _right= Array ();    $html _left = Array ();    $html _right = Array ();    $obj = Array ();    $loop = 0;        foreach ($data as $node) {$pid = $node [' FID '];        if (!isset ($child [$pid])) {$child [$pid] = array ();        } array_push ($child [$pid], $node [' id ']);    $obj [$node [' id ']] = $node;        } while (count ($stack) > 0) {$id = $stack [0];        $flag = false; $node = Isset ($obj [$id])?        $obj [$id]: null;            if (Isset ($child [$id])) {$cids = $child [$id];            $length = count ($cids);            for ($i = $length-1; $i >= 0; $i-) {Array_unshift ($stack, $cids [$i]);            } $obj [$cids [$length -1]][' islastchild '] = true;            $obj [$cids [0]][' isfirstchild '] = true;        $flag = true; if ($id! = $fid && $node &&!isset ($added _left[$id])) {IF (isset ($node [' Isfirstchild ']) && isset ($node [' islastchild ']) {$html _left[] = ' '; } else if (Isset ($node [' isfirstchild ')) {$html _left[] = ''; } else if (Isset ($node [' islastchild ')) {$html _left[] = ''; } else {$html _left[] = ''; } $html _left[] = ($flag = = = True)? "{$node [' title ']}":" {$node [' title ']} ";        $added _left[$id] = true; if ($id! = $fid && $node &&!isset ($added _right[$id])) {$html _right[] = ($flag = = = True)? '' : '';        $added _right[$id] = true;                if ($flag = = False) {if ($node) {$cids = $child [$node [' FID ']]; for ($i = count ($cids)-1; $i >= 0; $i-) {if ($cids [$i] = = $id) {array_sp                        Lice ($child [$node [' FID '], $i, 1);                    Break                }} if (count ($child [$node [' fid ']] = = 0) {$child [$node [' fid ']] = null;            }} array_push ($html _left, Array_pop ($html _right));        Array_shift ($stack);        } $loop + +;    if ($loop >) return $html _left;    } unset ($child);    Unset ($obj); Return implode (", $html _left);}    function Get_tree_option ($data, $fid) {$stack = array ($FID);    $child = Array ();    $added = Array ();    $options = Array ();    $obj = Array ();    $loop = 0;    $depth =-1;        foreach ($data as $node) {$pid = $node [' FID ']; If(!isset ($child [$pid]))        {$child [$pid] = array ();        } array_push ($child [$pid], $node [' id ']);    $obj [$node [' id ']] = $node;        } while (count ($stack) > 0) {$id = $stack [0];        $flag = false; $node = Isset ($obj [$id])?        $obj [$id]: null; if (Isset ($child [$id])) {for ($i = count ($child [$id])-1; $i >= 0; $i-) {Array_unshift ($st            Ack, $child [$id] [$i]);        } $flag = true;            if ($id! = $fid && $node &&!isset ($added [$id])) {$node [' depth '] = $depth;            $options [] = $node;        $added [$id] = true;        } if ($flag = = True) {$depth + +;                    } else {if ($node) {for ($i = count ($child [$node [' FID ']])-1; $i >= 0; $i-) {                        if ($child [$node [' FID ']][$i] = = $id) {array_splice ($child [' FID '], $node, 1);             Break       }} if (count ($child [$node [' fid ']] = = 0) {$child [$node [' fid ']] =                    Null                $depth--;        }} array_shift ($stack);        } $loop + +;    if ($loop >) return $options;    } unset ($child);    Unset ($obj); return $options;}? >PHP recursion problem (infinite pole classification)foreach itself is the equivalent of a judge, when the $arr array is not empty, foreach iterates and recursively accesses the child nodes, but for the leaf nodes, the $arr array is empty and will not be foreach at this point, and return directly. Got it?PHP recursion problem, want to display the data in the style of infinite pole classificationFirst, the JSON is decoded into the array, using the Json_decode function to note that the second parameter must be added otherwise he will return an object. The next step is to recursion. This is one of the simplest recursion as long as it is traversed individually.The following is the complete code:$data = Json_decode ($str, true), $options = GetChildren ($data), function GetChildren ($parent, $deep =0) {foreach ($parent As $row) {$data [] = array ("id" + = $row [' id '], "name" = + $row [' name '], "pid" + $row [' parentid '], ' deep ' = $deep) if ($row [' Childs ']) {$data = Array_merge ($data, GetChildren ($row [' Childs '], $deep + 1)}} return $data;}? >
        </option>!--? php}?--></select> 
    The above code has been tested as follows



     

    http://www.bkjia.com/phpjc/831802.html www.bkjia.com True http://www.bkjia.com/phpjc/831802.html techarticle php the implementation of the Infinite Pole classification (no recursion is used), PHP Recursive infinite Pole classification is often used in development, for example: Department structure, article classification. The difficulty with infinite pole classification is the output and the search ...

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.