首页 技术 正文
技术 2022年11月10日
0 收藏 645 点赞 4,786 浏览 2049 个字
<?phpclass Classification {
const PARENT_ID = 'parentid';
const ID = 'id';
const CHILDREN = 'children';public static function getTree($items) {
$children = [];
// group by parent id
foreach ($items as &$item) {
$children[ $item[self::PARENT_ID] ][] = &$item;
unset($item);
}
foreach ($items as &$item) {
$pid = $item[self::ID];
if (array_key_exists($pid, $children)) {
$item[self::CHILDREN] = $children[ $pid ];
}
unset($item);
}
return $children[0];
}
}

  test:

<?php$items = [
['id' => 1, 'parentid' => 0, 'name' => 'PHP'],
['id' => 2, 'parentid' => 1, 'name' => 'PHP_Framework'],
['id' => 42, 'parentid' => 1, 'name' => 'DevTools'],
['id' => 3, 'parentid' => 2, 'name' => 'ThinkPHP5'],
['id' => 4, 'parentid' => 2, 'name' => 'Laravel'],
['id' => 43, 'parentid' => 42, 'name' => 'PHPStorm'],
['id' => 44, 'parentid' => 42, 'name' => 'EclipsePDT'],
];
shuffle($items);echo '<pre>';
$a = array_map(function($item) {
return $item['name'];
}, $items);
print_r($a);$t = Classification::getTree($items);
var_dump($t);
// echo json_encode($t);

  

Thinkphp Model

<?php
namespace app\model;
use think\Model;
class Link extends Model {
protected $pk = 'id';
protected $field = ['des', 'source', 'target', 'structid'];
/**
* @param $items
* $items = array(
* array('id' => 42, 'parentid' => 1),
* array('id' => 43, 'parentid' => 42),
* array('id' => 1, 'parentid' => 0));
* @return mixed
* Array (
* [0] => Array(
* [id] => 1
* [parentid] => 0
* [childs] => Array(
* [0] => Array (
* [id] => 42
* [parentid] => 1
* [childs] => Array(
* [0] => Array(
* [id] => 43
* [parentid] => 42
* )
* )
* )
* )
* )
* )
*/
public static function buildTree($items) {
$childs = array();
foreach($items as &$item) {
$childs[$item['parentid']][] = &$item;
unset($item);
}
foreach($items as &$item) {
if (isset($childs[$item['id']])) {
$item['childs'] = $childs[$item['id']];
}
unset($item);
}
return $childs[0];
}
/**
* 节点id下一级节点id数组
*/
public function listChildNodeId_r($nodeid) {
$a = $this->listChildNodeId($nodeid);
$list = []; // recursively
foreach ($a as $it) {
array_push($list, $it);
$ca = $this->listChildNodeId($it);
foreach ($ca as $ci) {
array_push($list, $ci);
}
}
return $list;
}
// 节点id下一级节点id数组
private function listChildNodeId($parentid) {
$where = ['source' => $parentid];
$a = $this->field('target')->where($where)->select();
$t = [];
foreach ($a as $item) {
array_push($t, $item['target']);
}
unset($a);
return $t;
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292