Database value three-level classification background traversal

Source: Internet
Author: User

/**
*Converts a one-dimensional array of labels read from a database into a tag tree form
*Depending on the value of eachPARENT_ID,Leaves that change to the corresponding value
*@paramsArray $tagListOne-dimensional array read from the database,Each value of the array containsIdAndparent_id
* int $rootThe parent of the root nodeID string $idDefault isIdColumn NameString $pidDefault isparent_idColumn NameString $childIndex of Branch Point
*@returnArray $treeA label tree for multidimensional arrays
* */
static functionGetdictionarytree ($tagList,$root =0,$id =' ID ',$pid =' ParentID ',$child =' Child ') {
$tagTree =Array ();//Tree
if (Is_array ($tagList)) {
$array =Array ();
foreach ($tagListAs$key =$item) {
$array [$item [$id]] = &$tagList [$key];//Extracts the address of each element in the array in memory
}

foreach$tagListAs$key =$item) {
$parentId =$item [$pid];
If$root = =$PARENTID) {
$tagTree [] = &$tagList [$key];
}else{
IfIsset$array [$PARENTID])) {
$parent = &$array [$PARENTID];//Find the parent directlyIdIn-memory address,Then add child nodes
$parent [' node '] =True;NodeForTrueIndicates that there are child nodes under it,Convenient front desk Display this item can be expanded
$parent [$child [] = &$tagList [$key];
}
}
}
}
Return$tagTree;
}

/**
multidimensional array to one-dimensional array, To delete a label, delete the parent tag, all the child tag IDs get the
* @params array $tree The information read from the database bool $getTagName returns the label name, FALSE only returns the label ID
* BOOL $getGr Ouptag whether to return the Child Group label (valid only when the label name is returned) array $ret the initial value of the recursive call
* @return Array $ret The list of IDs returned, or Id=>array (' TagName ' =>name, ' ta GType ' =>val ' array list, Tattype 0 for normal tag 1 for group Tag * */

static function Array_multi_to_single ($tree, $getTagName = Fals      E, $getGroupTag = true, $ret = Array ())
{
foreach ($tree as $k + $v) {
if (!empty ($v [' child ')]) {
$ret = Self::array_multi_to_single ($v [' Child '], $getTagName, $getGroupTag, $ret);
}
if ($getTagName) {
if (isset ($v [' Child '])} {
if ($getGroupTag) {
$ret [$v [' id ']] = $v [' name '];
}
}else{
$ret [$v [' id ']] = $v [' name '];  
}
}else{
Array_push ($ret, $v [' id ']);
}
}
return < Span style= "color: #cc7832;" >< Span style= "color: #9876aa;" >< Span style= "color: #629755; Font-style:italic; " > $ret;}

Database value three-level classification background traversal

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.