mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKit.git
synced 2026-01-15 00:35:08 +08:00
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace Singularity\HDK\Utils\Service;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Generator;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
@@ -19,6 +21,12 @@ class UtilsService
|
||||
*/
|
||||
private RequestInterface $request;
|
||||
|
||||
/**
|
||||
* @Inject()
|
||||
* @var \Hyperf\Contract\StdoutLoggerInterface
|
||||
*/
|
||||
private StdoutLoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*
|
||||
@@ -163,4 +171,53 @@ class UtilsService
|
||||
fclose($file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* (level倒序)快速无极分类(时间复杂度 O(n),空间复杂度 O(1))
|
||||
* 条件:数组索引是数据parent_id对应的id,只支持level倒序
|
||||
*
|
||||
* @param array $list
|
||||
* @param int $level
|
||||
* @param string $parentIdName
|
||||
* @param string $childrenName
|
||||
* @param \Closure|null $filterCallback
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unlimitedSubCategoriesQuicklyWithLevel(
|
||||
array &$list,
|
||||
int $level = 1,
|
||||
string $parentIdName = 'parent_id',
|
||||
string $childrenName = 'children',
|
||||
?Closure $filterCallback = null
|
||||
): void {
|
||||
$start_time = microtime(true);
|
||||
$max_times = count($list);
|
||||
$current_times = 0;
|
||||
foreach ($list as $i => &$item) {
|
||||
$current_times++;
|
||||
if (!isset($item[$parentIdName])) {
|
||||
break;
|
||||
}
|
||||
|
||||
unset($list[$i]);
|
||||
$item = isset($filterCallback) ? $filterCallback($item) : $item;
|
||||
//判定非顶级的pid是否存在,如果存在,则再pid所在的数组下面加入一个字段items,来将本身存进去
|
||||
if (isset($list[$item[$parentIdName]])) {
|
||||
$list[$item[$parentIdName]][$childrenName][] = $item;
|
||||
continue;
|
||||
}
|
||||
|
||||
//取出顶级
|
||||
if ($item['level'] === $level) {
|
||||
$list[$childrenName][] = $item;
|
||||
}
|
||||
}
|
||||
$list = $list[$childrenName];
|
||||
$end_time = microtime(true);
|
||||
$this->logger->debug("快速无极分类循环{$current_times}次,数组元素数量$max_times");
|
||||
|
||||
$cost_time = ($end_time - $start_time) * 1000;
|
||||
$this->logger->debug("快速无极分类用时{$cost_time}ms");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user