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];
}
/*
Key initialization parameters:
$level: current child node depth
$last_cat_id: current parent node ID
$options: array with indentation level
$cat_id_array: along the same The parent nodes of the path are stationed in order
$level_array: the depth of the child nodes of the node is also stationed in order
*/
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, it will no longer be traversed
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 parent 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]);//After traversing, it will no longer be traversed
//If the current node has children, the current node will be stationed, but it will no longer be traversed; On the contrary, it will not be traversed without entering
if ($value['has_children'] > 0)
{
if (end($cat_id_array) != $last_cat_id)
{
$cat_id_array[] = $last_cat_id;
}
$last_cat_id = $cat_id;//When the current node becomes the new parent node of the next-level node
$cat_id_array[] = $cat_id;//Settled in
$level_array[$last_cat_id] = ++$level; //The depth of the next level node of the current node
}
}
elseif ($value['parent_id'] > $last_cat_id)
{//If the depth of the current node's parent is greater than the depth of the current parent node, proceed Next cycle
break;
}
}//endforeach
$count = count($cat_id_array);
if ($count > 1)
{
//Take out the last parent node entered 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 father node and there is no action Take it out when the current parent node is
$last_cat_id = end($cat_id_array);
}
else
{ // Otherwise, the last parent node taken out 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 have been saved starting from the root node An array with hierarchical properties for all nodes
$cat_options[0] = $options;
}
else
{
$options = $cat_options[0];
}
//If you start from 0, take the entire tree Then return 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 just say a few. The meaning of the parameters
/*
$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 is sorted like this: by the parent node Size, according to the direct parent node, according to the same parent node, such as root traversal, here is an example:
The first-level node has 1,5, the second-level node has 2,6,7, and the third-level node has 8,9 , if the direct children of 1 are 2,6 and the direct children of 2 are 8,9; in addition, the direct children of
5 are 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;
}
}
The above introduces the Chinese transliteration of electric shock PHP unlimited classification learning reference analysis of the infinite classification of ecshop with detailed annotations, including the Chinese transliteration of electric shock. I hope it will be helpful to friends who are interested in PHP tutorials.