


PHP practical use of recursion to read unlimited categories of products
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>
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.
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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
