Copy CodeThe 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];
}
/*
Initialize key parameters:
$level: Current child node depth
$last _cat_id: Current parent Node ID
$options: An array with indentation levels
$cat _id_array: Follow the parent node of the same path in sequence
$level _array: The node's sub-node depth, which is also entered sequentially
*/
if (!isset ($cat _options[0))
{
$level = $last _cat_id = 0;
$options = $cat _id_array = $level _array = Array ();
while (!empty ($arr))//If the node that remains to be constructed continues to traverse
{
foreach ($arr as $key = $value)
{
$cat _id = $value [' cat_id '];
First Class 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 '];
Traverse the past and no longer traverse
Unset ($arr [$key]);
if ($value [' has_children '] = = 0)
{
Continue
}
$last _cat_id = Father node of $cat _id;//lower node
$cat _id_array = Array ($cat _id);
$level _array[$last _cat_id] = + + $level;
Continue
}
The parent node ID of the current node equals its previous 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]);//traverse over and no longer traverse
If the current node has a child, the current node will be stationed but no longer traversed;
if ($value [' Has_children '] > 0)
{
if (end ($cat _id_array)! = $last _cat_id)
{
$cat _id_array[] = $last _cat_id;
}
$last _cat_id = $cat _id;//The new Father node as the node of the next level
$cat _id_array[] = $cat _id;//stationed
$level _array[$last _cat_id] = + + $level;//The Next level node depth of the current node
}
}
ElseIf ($value [' parent_id '] > $last _cat_id)
{//If the current node's father depth is greater than the current father's node depth then the next round cycle
Break
}
}//endforeach
$count = count ($cat _id_array);
if ($count > 1)
{
Take out the last stationed Father node as the current Father node
$last _cat_id = Array_pop ($cat _id_array);
}
ElseIf ($count = = 1)
{
if ($last _cat_id! = End ($cat _id_array))
{
The Father node that was stationed only one time and did not take it out as the current father.
$last _cat_id = end ($cat _id_array);
}
Else
{//Otherwise the final Father node must be a first-class classification node.
$level = 0;
$last _cat_id = 0;
$cat _id_array = Array ();
Continue
}
}
if ($last _cat_id && isset ($level _array[$last _cat_id]))
{
Take out the depth of the current node.
$level = $level _array[$last _cat_id];
}
Else
{
$level = 0;
}
}//end while, the non-recursive pre-order traversal of the construction tree has been completed, where $options has saved an array of all nodes starting from the root node with a layered nature
$cat _options[0] = $options;
}
Else
{
$options = $cat _options[0];
}
If the entire tree is taken from 0 onwards, it is returned to no longer be processed.
if (! $spec _cat_id)
{
return $options;
}
Otherwise start from the specified node interception, the following is relatively simple I would like to say a little bit, to say just a few parameters meaning it
/*
$spec _cat_id_level: The depth of the Intercept node
$spec _cat_id_array: A product Classification tree that eventually returns a root node of the node
The resulting array is sorted in such a way that the Father node size, according to the direct Father node, according to the same father node such as the first root traversal, with an example:
The first level node has 1, the 52 level node has the 2,6,7 three level node has 8, 9, if 1 's direct child is 2,6 and 2 's direct child is 8, 9;
5 of direct children are 7 so the final array is so arranged 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 describes the electric shock Chinese transliteration PHP infinite class classification Learning reference to the Ecshop infinite class analysis with detailed comments, including electric shock Chinese transliteration aspects of the content, I hope that the PHP tutorial interested in a friend helpful.