1
public function tree($array, $pid=0)
{
$tree = array();
foreach ($array as $key => $value) {
if ($value['parent_id'] == $pid) {
$value['child'] = $this->tree($array, $value['id']);
if (!$value['child']) {
unset($value['child']);
}
$tree[] = $value;
}
}
return $tree;
}
2
function getChild($data, $id = 0)
{
$child = [];
foreach ($data as $key => $datum) {
if ($datum['parent_id'] == $id) {
$child[$datum['id']] = $datum;unset($data[$key]);
$child[$datum['id']]['child'] = $this->getChild($data, $datum['id']);
}
}
return $child;
}
public function Index($request){
$DictionaryIndex = new DictionaryIndex();
$result = $DictionaryIndex->get()->toArray();
$data= $this->getChild($result);
return $data;
}
3、
public function getTree($types, $key = 0)
{
$tree = [];
$i = 0;
if (!is_array($types)) $types = $types->toArray();
if (isset($types[$key])) {
foreach ($types[$key] as $pid => $type) {
if (isset($type['mcode'])) $type['value'] = $type['mcode'];
if (isset($type['mname'])) $type['label'] = $type['mname'];
$tree[$i] = $type;
if (isset($types[$type['mcode']])) {
$children = $this->getTree($types, $type['mcode']);
$tree[$i]['children'] = $children;
}
$i++;
}
}
return $tree;
}
4
static function array_tree(array $data, $parent_id = 0, $level = 0)
{
static $_tmp = array();foreach ($data as $key => $item) {
if ($item['parent_id'] == $parent_id) {
foreach ($data as $k => $value) {
if ($value['parent_id'] == $item['id']) { //如果子级权限的parend_id等于顶级权限的id就说明这个权限是顶级权限的子级权限
$item['children'][] = $value; //然后把子权限放在一个新的数组里
}
}$_tmp[] = $item;
}
}return $_tmp;
}