Two ways to implement unlimited categorization in PHP

Source: Internet
Author: User
This article is divided into two cases, introduced in recursion and do not use recursion in the case of Php Unlimited class classification, PHP infinite Classification in our development process is very long to see, our future development will be very often used in PHP unlimited classification, then we should have mastered it, Small partners who do not know how to achieve unlimited classification of PHP can refer to this article

PHP for such a long time, found that the background management system is not a small application module is the classification of the column, in general, the columns are to be made unlimited, that is, each column theoretically can add sub-columns. In my opinion this kind of situation is not very complex to deal with, the only relatively difficult point is the infinite column query.

In this case I would like to make a simple introduction, for this kind of infinite column query generally there are two ways, one is to use the mechanism of the stack, the other is the use of recursive functions (of course, the recursive function implementation mechanism is also through the implementation of the stack). In the two ways below we are introduced separately.

Recursive function Implementation method

As mentioned above, recursive functions are also implemented by means of stacks, but the underlying processing of stacks is transparent to programmers, and programmers only need to care about the implementation logic of the application. So using recursion to deal with the above problems is easier to understand and more concise.

Now that we use recursive functions, we know that we have to rely on custom functions to look at names. Let me say a little bit about the implementation of the idea, the details we reflected in the code.

For each layer of the function its main work is to find the parent ID is the current ID of the column, find to call their own function again later, will find the ID of the column as the next layer of the parent ID.

The flowchart is as follows

Figure A

Do not know for the above explanation we can understand, it's okay we look directly below the code

<?php$channels = array (' ID ' =>1, ' name ' = ' "clothes", ' Parid ' =>0), array (' ID ' =>2, ' name ' = ' "book", '  Parid ' =>0), array (' ID ' =>3, ' name ' = "T-shirt", ' Parid ' =>1), array (' ID ' =>4, ' name ' = ' "Pants", ' Parid ' =>1), Array (' ID ' =>5, ' name ' = ' "shoes", ' Parid ' =>1), array (' ID ' =>6, ' name ' = "shoes", ' Parid ' =>5), array (' id ' = >7, ' name ' + ' sneakers ', ' Parid ' =>5), array (' ID ' =>8, ' name ' = ' "Nike", ' Parid ' =>7), array (' ID ' =>9, ' name ' = > "Nike", ' Parid ' =>3), array (' ID ' =>10, ' name ' = ' "Hung Star", ' Parid ' =>7), array (' ID ' =>11, ' name ' = ' "novel", ' Parid ' =>2), array (' ID ' =>12, ' name ' = "Science Fiction", ' Parid ' =>11), array (' ID ' =>13, ' name ' = ' "Classical masterpiece", ' parid ' = >11), array (' ID ' =>14, ' name ' = "Literature", ' Parid ' =>2), array (' ID ' =>15, ' name ' = "Classics", ' Parid ' =>14)); $html = Array ();/** * recursively finds nodes with a parent ID of $parid * @param array $html the found node according to the structure of the parent-child @param int $parid The specified parent ID * @param ARRA Y $channels Data array * @param int $DEP The depth of traversal, initialized to 1 */function getchild (& $html, $paRID, $channels, $DEP) {/* * Iterate through the data to find the ID specified by Parid for parameter $parid */for ($i = 0; $i <count ($channels); $i + +) {if ($channels [$i] [' parid '] = = $parid) {$html [] = array (' id ' = + $channels [$i] [' ID '], ' name ' = ' + ' $channels [$i] [' name '], ' dep ' = $DEP)      ;    Getchild ($html, $channels [$i] [' ID '], $channels, $DEP + 1); }}}getchild ($html, 0, $channels, 1);? >

This is the recursive implementation of infinite-level column query core code, combined with the diagram of its implementation process should have a clearer understanding.

Non-recursive, that is, using the stack mechanism to achieve infinite column query

In the above we probably introduced the use of recursive way to achieve infinite columns of the query, below we briefly introduce the non-recursive way. Although it is not possible to use recursive functions, it is necessary to solve this problem by referring to the mechanism of recursive implementation of the structure page of infinite columns.

In school when the teacher said, in fact, the core mechanism of the stack is four words: Advanced after the.

In this for the stack of the mechanism is not much to say, mainly about how to use the stack to achieve infinite level column query.

1. First press the top column into the stack

2. Stack top elements out of the stack

3. Place the stack element into the array, marking its depth (the depth is the depth of the parent column plus 1)

4. Take the element of the stack as the parent column and find its sub-columns

5. Move the found sub-columns into the stack and repeat steps 2

6. If the stack is empty, the process ends;

By translating the above steps, you can translate these steps into PHP code with the following core code:

<?php$channels = array (' ID ' =>1, ' name ' = ' "clothes", ' Parid ' =>0), array (' ID ' =>2, ' name ' = ' "book", '  Parid ' =>0), array (' ID ' =>3, ' name ' = "T-shirt", ' Parid ' =>1), array (' ID ' =>4, ' name ' = ' "Pants", ' Parid ' =>1), Array (' ID ' =>5, ' name ' = ' "shoes", ' Parid ' =>1), array (' ID ' =>6, ' name ' = "shoes", ' Parid ' =>5), array (' id ' = >7, ' name ' + ' sneakers ', ' Parid ' =>5), array (' ID ' =>8, ' name ' = ' "Nike", ' Parid ' =>7), array (' ID ' =>9, ' name ' = > "Nike", ' Parid ' =>3), array (' ID ' =>10, ' name ' = ' "Hung Star", ' Parid ' =>7), array (' ID ' =>11, ' name ' = ' "novel", ' Parid ' =>2), array (' ID ' =>12, ' name ' = "Science Fiction", ' Parid ' =>11), array (' ID ' =>13, ' name ' = ' "Classical masterpiece", ' parid ' = >11), array (' ID ' =>14, ' name ' = "Literature", ' Parid ' =>2), array (' ID ' =>15, ' name ' = "Classics", ' Parid ' =>14)); $stack = Array ();  Define an empty stack $html = Array (); Used to save the relationship between the columns and the depth of the column */* custom into the stack function */function pushstack (& $stack, $channel, $DEP) {Array_push ($stack, Array (' Channel ' = ' $channel, ' dep ' = $DEP));} /* * Custom outStack function */function popstack (& $stack) {return array_pop ($stack);} /* * First press the top column into the stack */foreach ($channels as $key + = $val) {if ($val [' parid '] = = 0) pushstack ($stack, $val, 0);} /* * Stack elements are stacked to find their sub-columns */do{$par = Popstack ($stack);//Put the top element of the stack */* to find the ID of the parent column in this column, put these columns into the stack */for ($i =0; $i <count ($ Channels) ($i + +) {if ($channels [$i] [' parid '] = = $par [' channel '] [' ID ']) {pushstack ($stack, $channels [$i], $par [' DEP ']+1    ); }/* * Save the column of the stack and the depth of the column to the array */$html [] = array (' id ' = ' = ' $par [' channel '] [' id '], ' name ' = ' = ' [' Channel '] [' name '], ' DEP ' = $par [' dep '];} while (count ($stack) >0);

The above is implemented using non-recursive methods.

Download code: HTTPS://GITHUB.COM/ONMPW/PHPAPP

Summarize

The above two ways have pros and cons, although the implementation of the form above, but given the structure of the infinite column, both the implementation of the mechanism are the same-all by means of the stack to achieve. In the real situation, we need to choose a way to implement according to the real situation.

Recommended articles:

PHP Implements infinite classification tree

How does PHP implement an infinite classification tree? This article mainly introduces PHP simple implementation of the infinite Classification tree list of the square ...

PHP Unlimited classification of the development process and case analysis

In our daily work, often encounter a lot of classification, we know that many open source software Unlimited classification is the use of recursive algorithm ...

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.