$array = array(
array('id' => 1, 'pid' => 0, 'n' => '河北省'),
array('id' => 2, 'pid' => 0, 'n' => '北京市'),
array('id' => 3, 'pid' => 1, 'n' => '邯郸市'),
array('id' => 4, 'pid' => 2, 'n' => '朝阳区'),
array('id' => 5, 'pid' => 2, 'n' => '通州区'),
array('id' => 6, 'pid' => 4, 'n' => '望京'),
array('id' => 7, 'pid' => 4, 'n' => '酒仙桥'),
array('id' => 8, 'pid' => 3, 'n' => '永年区'),
array('id' => 9, 'pid' => 1, 'n' => '武安市'),
array('id' => 10, 'pid' => 8, 'n' => '永年区镇'),
array('id' => 11, 'pid' => 0, 'n' => '上海市')
function getTree($array, $pid=0){
$tree = array();
foreach ($array as $key => $value) {
if ($value['pid'] == $pid) {
$value['children'] = getTree($array, $value['id']);
$tree[] = $value;
return $tree;
$list=getTree($array);
echo "<pre>";
print_r($list);
echo "</pre>";
第二种:bm规则(3,6,9,12…)
$array = array(
array('name' => '固定资产', 'bm' => '001'),
array('name' => '桌子', 'bm' => '001001'),
array('name' => '办公桌', 'bm' => '001001001'),
array('name' => '1米长办公桌', 'bm' => '001001001001'),
array('name' => '2米长办公桌', 'bm' => '001001001002'),
array('name' => '3米长办公桌', 'bm' => '001001001003'),
array('name' => '椅子', 'bm' => '001002'),
array('name' => '普通靠背椅', 'bm' => '001002001'),
array('name' => '塑料靠背椅', 'bm' => '001002002'),
array('name' => '空调', 'bm' => '001003'),
function getTree($array, $bm='001'){
$tree = array();
foreach ($array as $k => $v) {
$aaa = strpos($v['bm'], $bm);
$length = strlen($bm)+3;
if ($aaa == 0 && $aaa !== false && strlen($v['bm']) == $length ) {
$v['children'] = getTree($array, $v['bm']);
$tree[] = $v;
return $tree;
$list=getTree($array);
echo "<pre>";
print_r($list);
echo "</pre>";
第三种:点切割匹配
$array = array(
array('name' => '固定资产', 'bm' => '1.1'),
array('name' => '桌子', 'bm' => '1.1.1'),
array('name' => '办公桌', 'bm' => '1.1.1.1'),
array('name' => '1米长办公桌', 'bm' => '1.1.1.1.1'),
array('name' => '2米长办公桌', 'bm' => '1.1.1.1.2'),
array('name' => '3米长办公桌', 'bm' => '1.1.1.1.3'),
array('name' => '椅子', 'bm' => '1.1.2'),
array('name' => '普通靠背椅', 'bm' => '1.1.2.1'),
array('name' => '塑料靠背椅', 'bm' => '1.1.2.2'),
array('name' => '空调', 'bm' => '1.1.3'),
function getTree($data, $bm='1.1'){
$tree = array();
foreach ($data as $key => $value) {
$count1 = count(explode('.',$bm))+1;
$count2 = count(explode('.',$value['bm']));
if ($count1 == $count2 && strpos($value['bm'], $bm) === 0) {
$value['children'] = getTree($data, $value['bm']);
$tree[] = $value;
return $tree;
$list=getTree($array);
echo "<pre>";
print_r($list);
echo "</pre>";
第一种:pid找上级id$array = array( array('id' => 1, 'pid' => 0, 'n' => '河北省'), array('id' => 2, 'pid' => 0, 'n' => '北京市'), array('id' => 3, 'pid' => 1, 'n' => '邯郸市'), array('id' => 4, 'pid' => 2, 'n' => '朝阳区'),
领接表方式
主要依赖于一个 parent 字段,用于指向上级节点,将相邻的上下级节点连接起来,id 为自动递增自动,parent_id 为上级节点的 id。一目了然,“Java”是“Language”的子节点。
我们要显示树,PHP 代码也可以很直观,代码如下:
复制代码 代码如下:
<?php
* 获取父节点下的所有子节点
* @since 2011-05-18
* @param $par
//2.定义递归查询 数据格式化
public function data($pid=0){
$data = \DB::table('types')->where('pid',$pid)->get();
foreach ($data as $key=>$val){
$val->zi = $this->data($val->id);
function get_tree($data, $id_key = 'org_id', $pid_key = 'org_pid', $pid = 0, $deep = 0)
$tree = [];
foreach ($data as $row) {
if ($row[$pid_key] == $pid) {
$row['deep'] =...
public function sort_data($data, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0)
// 创建Tree
$tree = [];
if (!is_array($data)) {
return false;
一个类,可以遍历一个目录,将该目录下所有文件以及子目录及其文件都遍历,生成一个层次分明的数组,还可以将遍历的结果生成一个树状的字符串,直接echo到浏览器。
|-|a.txt
|-|b.txt
|-|c目录
|---|d.txt
|---|c1目录
|-----|c11.txt
|--|e目录
代码里有完整的用法示例。
function generateTreeData(flatData, parentId = null) {
const treeData = [];
flatData.forEach(item => {
if (item.parentId === parentId) {
const children = generateTreeData(flatData, item.id);
if (children.length) {
item.children = children;
treeData.push(item);
return treeData;
以上代码接受两个参数:`flatData` 表示扁平的数据,`parentId` 表示当前节点的父节点ID。在函数内部,我们首先创建空的数组 `treeData`,然后遍历 `flatData` 中的每个元素。如果当前元素的 `parentId` 与指定的 `parentId` 相同,就将其添加到 `treeData` 中,并
递归地调用 `generateTreeData` 函数来获取其子节点。最后返回 `treeData`。
要使用上述函数,只需要传入扁平的数据和根节点的ID,如下所示:
```javascript
const flatData = [
{ id: 1, name: '节点1', parentId: null },
{ id: 2, name: '节点2', parentId: 1 },
{ id: 3, name: '节点3', parentId: 1 },
{ id: 4, name: '节点4', parentId: 2 },
{ id: 5, name: '节点5', parentId: 2 },
{ id: 6, name: '节点6', parentId: 3 },
{ id: 7, name: '节点7', parentId: 3 },
const treeData = generateTreeData(flatData, null);
console.log(treeData);
以上代码将输出如下结果:
```javascript
"id": 1,
"name": "节点1",
"parentId": null,
"children": [
"id": 2,
"name": "节点2",
"parentId": 1,
"children": [
"id": 4,
"name": "节点4",
"parentId": 2,
"children": []
"id": 5,
"name": "节点5",
"parentId": 2,
"children": []
"id": 3,
"name": "节点3",
"parentId": 1,
"children": [
"id": 6,
"name": "节点6",
"parentId": 3,
"children": []
"id": 7,
"name": "节点7",
"parentId": 3,
"children": []
希望这个示例能够对你有所帮助!