Home Backend Development PHP Tutorial PHP practical use of recursion to read unlimited categories of products

PHP practical use of recursion to read unlimited categories of products

Jul 29, 2016 am 09:12 AM
array Level limit parent

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of how to use take and limit in Laravel Detailed explanation of how to use take and limit in Laravel Mar 10, 2024 pm 05:51 PM

"Detailed explanation of how to use take and limit in Laravel" In Laravel, take and limit are two commonly used methods, used to limit the number of records returned in database queries. Although their functions are similar, there are some subtle differences in specific usage scenarios. This article will analyze the usage of these two methods in detail and provide specific code examples. 1. Take method In Laravel, the take method is used to limit the number of records returned, usually combined with the orderBy method.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

How to access parent class properties in Python? How to access parent class properties in Python? Aug 26, 2023 am 10:17 AM

Inobject-orientedprogramming,inheritanceallowsustocreatenewclassesthatinheritthepropertiesandmethodsofanexistingclass.Thispowerfulconceptenablescodereuse,modularity,andextensibilityinourprograms.Beforedivingintoaccessingparentclassattributes,let'shav

A deep dive into the differences between take and limit in Laravel A deep dive into the differences between take and limit in Laravel Mar 10, 2024 pm 01:00 PM

In Laravel, we often use some methods to limit the number of query results, including take and limit methods. While they can both be used to limit the number of query results, they do have some subtle differences. In this article, we'll take a deep dive into how take and limit differ in Laravel, illustrating them with concrete code examples. First, let's look at the take method. The take method is part of Eloquent and is typically used for

Comparison of functions and usage of take and limit in Laravel Comparison of functions and usage of take and limit in Laravel Mar 09, 2024 pm 09:09 PM

Take and limit are two commonly used methods in Laravel to limit the number of query result sets. Although they have certain similarities in functionality, they differ in usage and some details. This article will conduct a detailed comparison of the functions and usage of the two methods, and provide specific code examples to help readers better understand the differences between them and how to apply them correctly. 1.take method The take method is in the LaravelEloquent query builder

How to use the limit and skip functions of Stream in Java for stream operations How to use the limit and skip functions of Stream in Java for stream operations Jun 26, 2023 pm 03:55 PM

StreamAPI was introduced in Java 8, which can greatly simplify the operation of collections. The Stream class provides many functional methods for operating on streams, including filtering, mapping, merging, and more. Among them, limit and skip are two functions used to limit the number of elements in stream operations. 1. Limit function The limit function is used to limit the number of elements in the stream. It accepts a long type parameter n, which represents the number of limits. After calling the limit function, a new stream is returned, which only contains

See all articles