The discussion on recursive algorithms is not yet graduated. now I am working on a project and have encountered recursive problems. I hope you can help me! The problem is very simple. the question of classical recursive traversal array & lt ;? Php $ arrarray (0 & gt; array (& nbsp; id & gt; 1, name & gt; role management, pid & gt; 0,), 1 & gt; array (& nbsp; id & g about recursive algorithms
The younger brother is not yet a graduate student. now he is working on an internship program and has encountered recursive problems. I hope you can help me!
The problem is very simple. the problem of classical recursive traversal of arrays
$ Arr = array (
'0' => array ('id' => '1', 'name' => 'role management', 'pid '=> '0 ',),
'1' => array ('id' => '2', 'name' => 'Add role ', 'pid' => '1 ',),
'2' => array ('id' => '3', 'name' => 'role list', 'pid '=> '2 ',),
'3' => array ('id' => '4', 'name' => 'menu management', 'pid '=> '0 ',),
'4' => array ('id' => '5', 'name' => 'foreground management', 'pid '=> '4 ',),
'5' => array ('id' => '6', 'name' => 'Add', 'pid '=> '5 ',),
'6' => array ('id' => '7', 'name' => 'modify', 'pid '=> '5 ',),
'7' => array ('id' => '8', 'name' => 'list', 'pid '=> '5 ',),
'8' => array ('id' => '9', 'name' => 'background management', 'pid '=> '4 ',),
'9' => array ('id' => '10', 'name' => 'Add', 'pid '=> '9 ',),
'10' => array ('id' => '11', 'name' => 'modify', 'pid '=> '9 ',),
'11' => array ('id' => '12', 'name' => 'list', 'pid '=> '9 ',),
);
?>
In this way, recursive traversal requires two results.
An array of standard hierarchical relationships
Another one is used to put it in the drop-down list,
In addition, I checked the information and found that pointers could be used. students are still very simple. please give me some advice !!!
------ Solution --------------------
/**
* A common tree class can generate any tree structure
*/
Header ("content-type: text/html; charset = utf-8 ");
Class tree {
/**
* The two-dimensional array required to generate a tree structure
* @ Var array
*/
Public $ arr = array ();
/**
* The modifier required to generate a tree structure, which can be replaced by an image.
* @ Var array
*/
Public $ icon = array ('│', 'hangzhou', 'hangzhou ');
Public $ nbsp = "";
/**
* @ Access private
*/
Public $ ret = '';
/**
* Constructor, initialization class
* @ Param array 2-dimensional array, for example:
* Array (
* 1 => array ('id' => '1', 'parentid' => 0, 'name' => 'Level 1 topic 1 '),
* 2 => array ('id' => '2', 'parentid' => 0, 'name' => 'Level 1 column 2 '),
* 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level column 1 '),
* 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '),
* 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '),
* 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Level 3 topic 1 '),
* 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-level column 2 ')
*)
*/
Public function _ construct ($ arr = array ()){
$ This-> arr = $ arr;
$ This-> ret = '';
Return is_array ($ arr );
}
/**
* Obtain the parent array.
* @ Param int
* @ Return array
*/
Public function get_parent ($ myid ){
$ Newarr = array ();
If (! Isset ($ this-> arr [$ myid]) return false;
$ Pid = $ this-> arr [$ myid] ['parentid'];
$ Pid = $ this-> arr [$ pid] ['parentid'];
If (is_array ($ this-> arr ))
{
Foreach ($ this-> arr as $ id => $)
{
If ($ a ['parentid'] = $ pid) $ newarr [$ id] = $;
}
}
Return $ newarr;
}
/**
* Obtain the child-level array.
* @ Param int
* @ Return array
*/
Public function get_child ($ myid)
{
$ A = $ newarr = array ();
If (is_array ($ this-> arr ))
{
Foreach ($ this-> arr as $ id => $)
{
If ($ a ['parentid'] ==$ myid) $ newarr [$ id] = $;
}
}
Return $ newarr? $ Newarr: false;
}
/**
* Obtain the array of the current position.
* @ Param int
* @ Return array
*/
Public function get_pos ($ myid, & $ newarr)
{
$ A = array ();
If (! Isset ($ this-> arr [$ myid]) return false;
$ Newarr [] = $ this-> arr [$ myid];
$ Pid = $ this-> arr [$ myid] ['parentid'];
If (isset ($ this-> arr [$ pid])
{
$ This-> get_pos ($ pid, $ newarr );
}
If (is_array ($ newarr ))
{
Krsort ($ newarr );
Foreach ($ newarr as $ v)
{
$ A [$ v ['id'] = $ v;
}
}
Return $;
}
/**
* Tree structure
* @ Param int ID, which indicates that all sub-levels under this ID are obtained.
* @ Param string: generates the basic code of the tree structure, for example :"\ $ Spacer \ $ name"
* @ Param int indicates the selected ID, for example, used in tree-type drop-down boxes.
* @ Return string
*/
Public function get_tree ($ myid, $ str, $ sid = 0, $ adds = '', $ str_group = '')
{
$ Number = 1;
$ Child = $ this-> get_child ($ myid );
Print_r ($ child );
If (is_array ($ child ))
{
$ Total = count ($ child );
Foreach ($ child as $ id => $ value)
{
$ J = $ k = '';
If ($ number = $ total)
{
$ J. = $ this-> icon [2]; // if the last child level is used
} Else