Converts the returned dataset to an array tree.
For example
$ Old = array (
Array ('id' => 1, 'pid '=> 0, 'name' => '1e '),
Array ('id' => 2, 'pid '=> 1, 'name' => 'second '),
Array ('id' => 3, 'pid '=> 2, 'name' => 'third '),
);
Print_r (list_to_tree ($ old, 'id', 'pid ',' _ child '));
Output:
$ Old = array (
Array (
'Id' => 1,
'Pid '=> 0,
'Name' => '1 ',
'_ Child' => array (
'Id' => 2,
'Pid '=> 1,
'Name' => 'second'
'_ Child' => array ('id' => 3, 'pid '=> 2, 'name' => 'third '),
),
),
);
- /**
- * Convert the returned dataset to a Tree.
- * @ Access public
- * @ Param array $ list the dataset to be converted
- * @ Param string $ pid parent tag field
- * @ Param string $ level field
- * @ Return array
- */
- Function list_to_tree ($ list, $ pk = 'id', $ pid = 'pid', $ child = '_ child', $ root = 0 ){
- // Create a Tree
- $ Tree = array ();
- If (is_array ($ list )){
- // Create a primary key-based array reference
- $ Refer = array ();
- Foreach ($ list as $ key => $ data ){
- $ Refer [$ data [$ pk] = & $ list [$ key];
- }
- Foreach ($ list as $ key => $ data ){
- // Determine whether a parent exists
- $ ParentId = $ data [$ pid];
- If ($ root = $ parentId ){
- $ Tree [] = & $ list [$ key];
- } Else {
- If (isset ($ refer [$ parentId]) {
- $ Parent = & $ refer [$ parentId];
- $ Parent [$ child] [] = & $ list [$ key];
- }
- }
- }
- }
- Return $ tree;
- }
|