-
-
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 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'];
- //It will no longer be traversed after it has been 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 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]);//After traversing, it will no longer be traversed
- //If the current node has children, the current node will be stationed, but no longer Traverse; otherwise, 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 is used as the new parent node of the next-level node
- $cat_id_array[] = $cat_id;//Enter
$ 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 to the next cycle
- break;
- }
- }//endforeach
- $count = count($cat_id_array);
- if ($count > ; 1)
- {
- //Take out 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 used as the current parent node, take it out
- $last_cat_id = end($cat_id_array);
- }
- else
- { //Otherwise, the last parent taken out The 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 and no longer processed.
- 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 bit. Let me talk about the meaning of a few parameters.
- /*
- $spec_cat_id_level: The depth of the intercepted node
- $spec_cat_id_array: The final return A product classification tree with this node as the root node
- The final returned array is sorted like this: by the size of the parent node, by the direct parent node, by the same parent node, and so on. 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 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;
- }
- }
- ?>
-
Copy code
|