Copy code The code is as follows:
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;
}
}
http://www.bkjia.com/PHPjc/321477.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321477.htmlTechArticle复制代码 代码如下: function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id])) { return $cat_options[$spec_cat_id]; } /...