2019-12-26 09:03:19 +08:00

25 lines
519 B
PHP

<?php
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);
if (count($child)) {
$item[$children] = $child;
}
$tree[] = $item;
}
}
return $tree;
}
}