


PHP infinite classification learning reference: analysis of ecshop infinite classification with detailed annotations_PHP tutorial
function cat_options($spec_cat_id, $arr)
{
static $cat_options = array ();
if (isset($cat_options[$spec_cat_id]))
{
return $cat_options[$spec_cat_id];
}
/*
Initialization key parameters:
$level: current child node depth
$last_cat_id: current parent node ID
$options: array with indentation level
$cat_id_array: parent nodes along the same path are stationed in sequence
$ level_array: The depth of the child nodes of this node is also entered in sequence
*/
if (!isset($cat_options[0]))
{
$level = $last_cat_id = 0;
$options = $cat_id_array = $level_array = array();
while (!empty($arr))//If there are still nodes to be constructed, continue traversing
{
foreach ($arr AS $key => $value)
{
$cat_id = $value['cat_id'];
//Level 1 classification node
if ($level == 0 && $last_cat_id == 0 )
{
if ($value['parent_id'] > 0)
{
break;
}
$options[$cat_id] = $value;
$options[$cat_id]['level'] = $level;
$options[$cat_id]['id'] = $cat_id;
$options[$cat_id]['name'] = $value ['cat_name'];
//After traversing, no longer traverse
unset($arr[$key]);
if ($value['has_children'] == 0)
{
continue;
}
$last_cat_id = $cat_id;//The parent node of the lower node
$cat_id_array = array($cat_id);
$level_array[$last_cat_id] = ++$level;
continue;
}
//The parent node ID of the current node is equal to its upper-level node ID
if ($value['parent_id'] == $last_cat_id)
{
$options[$cat_id] = $value;
$options[$cat_id]['level'] = $level;
$options[$cat_id]['id '] = $cat_id;
$options[$cat_id]['name'] = $value['cat_name'];
unset($arr[$key]);//No more after traversing Traversal
//If the current node has children, the current node will be stationed, but will not be traversed; otherwise, it will not be stationed and will not be traversed again
if ($value['has_children'] > 0)
{
if (end($cat_id_array) != $last_cat_id)
{
$cat_id_array[] = $last_cat_id;
}
$last_cat_id = $cat_id;//current node As the new parent node of the next level node
$cat_id_array[] = $cat_id;//Settled in
$level_array[$last_cat_id] = ++$level;//Current node The depth of the next level node
}
}
elseif ($value['parent_id'] > $last_cat_id)
{//If the depth of the current node's parent is greater than the current parent The depth of the node will proceed to the next cycle
break;
}
}//endforeach
$count = count($cat_id_array);
if ($count > 1)
{
//Get the last entered parent node as the current parent node
$last_cat_id = array_pop($cat_id_array);
}
elseif ($count == 1)
{
if ($last_cat_id != end($cat_id_array))
{
//When there is only one stationed parent node and it is not the current parent node, take it out
$last_cat_id = end($ cat_id_array);
}
else
{ // Otherwise, the last retrieved parent node must be a first-level classification node
$level = 0;
$last_cat_id = 0;
$cat_id_array = array();
continue;
}
}
if ($last_cat_id && isset($level_array[$last_cat_id]))
{
// Get the depth of the current node
$level = $level_array[$last_cat_id];
}
else
{
$level = 0;
}
}//end while, at this time, the work of non-recursive pre-order traversal to construct the tree has been completed, in which $options has saved a hierarchical array of all nodes starting from the root node
$cat_options[0] = $options;
}
else
{
$options = $cat_options[0];
}
//If the entire tree is taken starting from 0, it will be returned directly without processing.
if (!$spec_cat_id)
{
return $options;
}
//Otherwise, start intercepting from the specified node. The following is relatively simple. I will talk about it a little. I will say a few words. The meaning of each parameter
/*
$spec_cat_id_level: the depth of the intercepted node
$spec_cat_id_array: the final returned product classification tree with this node as the root node
The final returned array It is sorted like this: by the size of the parent node, by the direct parent node, and by the same parent node. This is the first root traversal. Here is an example:
The first-level nodes have 1,5 and the second-level nodes have 2, 6,7 The third-level node has 8,9. If the direct child of 1 is 2,6 and the direct child of 2 is 8,9; in addition, the direct child of
5 is 7, then the final array is arranged like this 1 ->2->8->9->6->5->7
*/
else
{
if (empty($options[$spec_cat_id] ))
{
return array();
}
$spec_cat_id_level = $options[$spec_cat_id]['level'];
foreach ($options AS $key = > $value)
{
if ($key != $spec_cat_id)
{
unset($options[$key]);
}
else
{
break;
}
}
$spec_cat_id_array = array();
foreach ($options AS $key => $value)
{
if (($ spec_cat_id_level == $value['level'] && $value['cat_id'] != $spec_cat_id) ||
($spec_cat_id_level > $value['level']))
{
break ;
}
else
{
$spec_cat_id_array[$key] = $value;
}
}
$cat_options[$spec_cat_id] = $spec_cat_id_array;
return $spec_cat_id_array;
}
}

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











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
