PHP array unlimited hierarchical data hierarchical processing code _ PHP Tutorial

Source: Internet
Author: User
The hierarchical processing code of PHP array infinitely hierarchical data. The copy code is as follows: * ** create a parent node tree array * parameter * $ ar array, data organized by the list of neighboring items * $ id array as the subscript or associated key name of the primary key * $ pid array The code is as follows:


/**
* Create a parent node tree array
* Parameters
* $ Ar array, data organized in the form of an adjacent list
* $ Subscripts or associated key names used as primary keys in the id array
* $ Subscript or associated key name of the parent key in the pid array
* Returns a multi-dimensional array.
**/
Function find_parent ($ ar, $ id = 'id', $ pid = 'pid '){
Foreach ($ ar as $ v) $ t [$ v [$ id] = $ v;
Foreach ($ t as $ k => $ item ){
If ($ item [$ pid]) {
If (! Isset ($ t [$ item [$ pid] ['parent'] [$ item [$ pid])
$ T [$ item [$ id] ['parent'] [$ item [$ pid] = & $ t [$ item [$ pid];
$ T [$ k] ['refer'] = true;
}
}
Return $ t;
}
/**
* Create a subnode tree array
* Parameters
* $ Ar array, data organized in the form of an adjacent list
* $ Subscripts or associated key names used as primary keys in the id array
* $ Subscript or associated key name of the parent key in the pid array
* Returns a multi-dimensional array.
**/
Function find_child ($ ar, $ id = 'id', $ pid = 'pid '){
Foreach ($ ar as $ v) $ t [$ v [$ id] = $ v;
Foreach ($ t as $ k => $ item ){
If ($ item [$ pid]) {
$ T [$ item [$ pid] ['child '] [$ item [$ id] = & $ t [$ k];
$ T [$ k] ['refer'] = true;
}
}
Return $ t;
}


Example:

The code is as follows:


$ Data = array (
Array ('id' => 1, 'parent' => 0, 'name' => 'grandpa '),
Array ('id' => 2, 'parent' => 1, 'name' => 'father '),
Array ('id' => 3, 'parent' => 1, 'name' => 'Uncle '),
Array ('id' => 4, 'parent' => 2, 'name' => 'your '),
Array ('id' => 5, 'parent' => 4, 'name' => 'son ')
);
$ P = find_parent ($ data, 'id', 'parent ');
$ C = find_child ($ data, 'id', 'parent ');


The above two methods are to spread all nodes to an array by id, find their parent or children, and link the elements to parent and children by referencing them,
However, the referenced elements still exist in an array. Therefore, in actual applications, it is best to mark those referenced elements to avoid traversal starting with them as the root, leading to repetition.

The code is as follows:


Foreach ($ p as $ key => $ item ){
If ($ item ['reference']) continue;
Print_r ($ item );
}
Foreach ($ c as $ key => $ item ){
If ($ item ['reference']) continue;
Print_r ($ item );
}


Recursive method: After the PHP array elements are deleted, the array cursor will return to zero. Therefore, some elements that have found a "destination" in the traversal process have to be left in the array, the search range of subsequent elements cannot be reduced:

The code is as follows:


$ Mylist = array ('parent _ id' => 0, 'id' => 1 ),
Array ('parent _ id' => 0, 'id' => 2 ),
Array ('parent _ id' => 0, 'id' => 3 ),
Array ('parent _ id' => 2, 'id' => 4 ),
Array ('parent _ id' => 2, 'id' => 5 ),
Array ('parent _ id' => 3, 'id' => 6 ),
Array ('parent _ id' => 3, 'id' => 7 ),
Array ('parent _ id' => 4, 'id' => 8 ),
Array ('parent _ id' => 5, 'id' => 9 ),
Array ('parent _ id' => 5, 'id' => 10)
);

Function _ findChildren ($ list, $ p_id) {// hierarchical data,
$ R = array ();
Foreach ($ list as $ id => $ item ){
If ($ item ['parent _ id'] = $ p_id ){
$ Length = count ($ r );
$ R [$ length] = $ item;
If ($ t = $ this-> _ findChildren ($ list, $ item ['id']) {
$ R [$ length] ['Children '] = $ t;
}
}
}
Return $ r;
}

Print_r (_ findChildren ($ mylist, 0 ));

The http://www.bkjia.com/PHPjc/326411.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326411.htmlTechArticle code is as follows:/*** create a parent node tree array * parameter * $ ar array, the data organized by the list of neighboring items * $ id array as the subscript or associated key name of the primary key * $ pid 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.