Generate a tree structure (such as provincial and municipal), and a tree structure for provincial and municipal areas. Generate a tree structure (for example, province and city), and a tree structure for province and city? Phpheader (Content-type: texthtml; charsetutf-8); functiongenerateTree ($ items) {$ treearray (); foreach ($ generate a tree structure (such as provincial/municipal), tree structure provincial/municipal
// Note that the key value of the array must be consistent with the id value $ items = array (1 => array ('id' => 1, 'pid '=> 0, 'name' => 'Anhui province'), 2 => array ('id' => 2, 'pid '=> 0, 'name' => 'Zhejiang province '), 3 => array ('id' => 3, 'pid' => 1, 'name' => 'Hefei '), 4 => array ('id' => 4, 'pid '=> 3, 'name' => 'changfeng County '), 5 => array ('id' => 5, 'pid '=> 1, 'name' => 'anqing'),); echo"";print_r(generateTree($items));
Optimization of the above method:
Array ('id' => 1, 'pid '=> 0, 'name' => 'Anhui province'), 2 => array ('id' => 2, 'pid '=> 0, 'name' => 'Zhejiang province'), 3 => array ('id' => 3, 'pid '=> 1, 'name' => 'Hefei '), 4 => array ('id' => 4, 'pid' => 3, 'name' => 'changfeng County '), 5 => array ('id' => 5, 'pid '=> 1, 'name' => 'anqing'),); echo"";print_r(generateTree($items));
View Code
Result:
What is the tree structure of a province (such as a province, city? Php header ("Content-type: text/html; charset = utf-8"); function generateTree ($ items) {$ tree = array (); foreach ($...