PHP Infinite Class Classification Learning reference the analytic band detailed annotation _php skill of ecshop Infinite class classification

Source: Internet
Author: User
Copy Code code 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: Arrays with indentation levels
$cat _id_array: The parent node in the same path is stationed sequentially
$level _array: The node's child node depth, is also in sequence
*/
if (!isset ($cat _options[0]))
{
$level = $last _cat_id = 0;
$options = $cat _id_array = $level _array = Array ();
while (!empty ($arr))//If the node that has yet to be constructed continues to traverse
{
foreach ($arr as $key => $value)
{
$cat _id = $value [' cat_id '];
First Grade 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 traversing.
Unset ($arr [$key]);
if ($value [' has_children '] = = 0)
{
Continue
}
$last _cat_id = Father node of the lower node $cat _id;//
$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]);//traverse over will no longer traverse
If the current node has children, the current node will be stationed, but no longer traverse;
if ($value [' Has_children '] > 0)
{
if (end ($cat _id_array)!= $last _cat_id)
{
$cat _id_array[] = $last _cat_id;
}
$last _cat_id = $cat _id;//as a new Father node for the next node
$cat _id_array[] = $cat _id;//stationed

$level _array[$last _cat_id] = + + $level;//The next node depth of the current node
}

}
ElseIf ($value [' parent_id '] > $last _cat_id)
{//If the current node father depth is greater than the current father's node depth then proceed to the next round of circulation.
Break
}
}//endforeach
$count = count ($cat _id_array);
if ($count > 1)
{
Remove 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 parent node that is stationed is only one time and does not take it out as the current parent.
$last _cat_id = end ($cat _id_array);
}
Else
{//Otherwise the final removal of the Father node must be a first-order classification node
$level = 0;
$last _cat_id = 0;
$cat _id_array = Array ();
Continue
}
}

if ($last _cat_id && isset ($level _array[$last _cat_id))
{
Remove the depth of the current node
$level = $level _array[$last _cat_id];
}
Else
{
$level = 0;
}
}//end while, this completes the work of a non-recursive forward traversal of the construction tree, 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, it is returned directly and no longer processed.
if (! $spec _cat_id)
{
return $options;
}
Otherwise start intercepting from the specified node, the following is relatively simple I'd like to say a few words about the meaning of the argument
/*
$spec _cat_id_level: The depth of the Intercept node
$spec _cat_id_array: A product Classification tree that is ultimately returned with the node as its root.
The final returned array is sorted in this way: according to the Father node size, according to the direct Father node, according to the same father node such as the root traversal, with an example:
The first grade node has 1, the 52 grade node has the 2,6,7 three grade node to have 8, 9, if 1 's direct child is 2,6 and 2 the direct child is 8, 9;
5 of the direct child is 7 then the final array is this sort of 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.