php无限分类 构建树形结构

<?php

class 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 appmodel;
use thinkModel;
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;
    }
}

  

原文地址:https://www.cnblogs.com/mingzhanghui/p/9168357.html