src/Models/TreeTrait.php
public static function datasetToTree(array $items, bool $idAsKey = true)
	{
		$node = new static;
		$ids = [];

		if ($idAsKey)
		{
			foreach ($items as $item)
			{
				if ($item[$node->getKeyName()] == $item[$node->parentKey]) continue; //如果父ID等于自己，避免死循环，跳过

				$ids[] = $item[$node->getKeyName()];
				$items[ ($item[$node->parentKey]) ][ 'children' ][ ($item[$node->getKeyName()]) ] = &$items[ ($item[$node->getKeyName()]) ];
			}
		}
		else
		{
			foreach ($items as $item)
			{
				if ($item[$node->getKeyName()] == $item[$node->parentKey]) continue; //如果父ID等于自己，避免死循环，跳过

				$ids[] = $item[$node->getKeyName()];
				$items[ ($item[$node->parentKey]) ][ 'children' ][] = &$items[ ($item[$node->getKeyName()]) ];
			}
		}

		$result = array_except($items, $ids);
		return count($result) === 1 ? array_get(array_pop($result), 'children', []) : $result;
	}
public static function datasetToTree(array $items, bool $idAsKey = true)
	{
		$node = new static;
		$ids = [];

		if ($idAsKey)
		{
			foreach ($items as $item)
			{
				if ($item[$node->getKeyName()] == $item[$node->parentKey]) continue; //如果父ID等于自己，避免死循环，跳过

				$ids[] = $item[$node->getKeyName()];
				$items[ ($item[$node->parentKey]) ][ 'children' ][ ($item[$node->getKeyName()]) ] = &$items[ ($item[$node->getKeyName()]) ];
			}
		}
		else
		{
			foreach ($items as $item)
			{
				if ($item[$node->getKeyName()] == $item[$node->parentKey]) continue; //如果父ID等于自己，避免死循环，跳过

				$ids[] = $item[$node->getKeyName()];
				$items[ ($item[$node->parentKey]) ][ 'children' ][] = &$items[ ($item[$node->getKeyName()]) ];
			}
		}

		$result = array_except($items, $ids);
		return count($result) === 1 ? array_get(array_pop($result), 'children', []) : $result;
	}
将二维dataset数组生成tree，必须是ID为KEY的二维数组

@param mixed $items ID为KEY的二维数组
@param bool 返回的children中，是否以ID为Key