27 lines
545 B
PHP
Raw Normal View History

2019-12-11 20:59:59 +08:00
<?php
2020-11-29 09:29:14 +08:00
declare(strict_types=1);
2019-12-12 09:14:08 +08:00
namespace catcher;
class Tree
{
public static function done(array $items, $pid = 0, $pidField = 'parent_id', $children = 'children')
{
$tree = [];
foreach ($items as $key => $item) {
if ($item[$pidField] == $pid) {
$child = self::done($items, $item['id'], $pidField);
2019-12-26 09:03:19 +08:00
if (count($child)) {
$item[$children] = $child;
}
2019-12-12 09:14:08 +08:00
$tree[] = $item;
}
}
return $tree;
}
}