PHP practical use of recursion to read unlimited categories of products

WBOY
Release: 2016-07-29 09:12:38
Original
1500 people have browsed it

When I was working on a mall project recently, I needed to take out the data in the classification table. The classification table was associated with each category through a pid (parent class id). In order to organize the read two-dimensional data into a tree format, I The following method is encapsulated in the project.

<code><span>/**
 * [treeCats description]
 *<span> @param</span>  [array]  $arr   [原始未排序的数组]
 *<span> @param</span>  [array]  $limit [第一个元素代表开始递归的parent_id,默认为0,第二个元素代表剔除元素及子元素cat_id,如果没有传值,默认为0]
 *<span> @param</span>  integer $level [函数调用的深度]
 *<span> @return</span> [array]         [排好序的数组]
 */</span><span>public</span><span><span>function</span><span>treeCats</span><span>(<span>$arr</span>,<span>$limit</span>,<span>$level</span>=<span>0</span>)</span>{</span><span>$rec</span> = <span>array</span>();
            <span>//先消除cat_id对应的值</span><span>foreach</span> (<span>$arr</span><span>as</span><span>$key</span>=><span>$value</span>){
            <span>if</span>(<span>$value</span>[<span>'cat_id'</span>]!=<span>$limit</span>[<span>'cd'</span>]){
                <span>$arr_new</span> [<span>$key</span>]=<span>$value</span>;
            }

        }
        <span>//对于新数组进行遍历</span><span>foreach</span> (<span>$arr_new</span><span>as</span><span>$key</span> => <span>$value</span>) {
        <span>if</span>(<span>$value</span>[<span>'parent_id'</span>]==<span>$limit</span>[<span>"pd"</span>] &&<span>$value</span>[<span>'parent_id'</span>]!=<span>$limit</span>[<span>"cd"</span>]){
                <span>$value</span>[<span>'level'</span>]=<span>$level</span>;
                <span>$rec</span> []= <span>$value</span>;
                <span>//生成用于下一层循环的数组</span><span>$next_limit</span> = <span>array</span>(<span>"pd"</span>=><span>$value</span>[<span>'cat_id'</span>],<span>"cd"</span>=><span>$limit</span>[<span>"cd"</span>]);

                <span>$rec</span> = array_merge(<span>$rec</span>,<span>$this</span>->treeCats(<span>$arr</span>,<span>$next_limit</span>,<span>$level</span>+<span>1</span>));
            }
        }       
        <span>return</span><span>$rec</span>;
    }
</code>
Copy after login

The application scenario of setting $limit here is that if the parent category of this category is modified to its original subcategory, this category branch will be lost, so at this time, its subcategories should be eliminated from the optional category.
PHP practical use of recursion to read unlimited categories of products

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the practical use of PHP to read unlimited categories of products recursively, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template