Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- class Tree
- {
- /**
- * @var array
- */
- private $records;
- public function __construct(array $records)
- {
- $this->records = $records;
- }
- public function build(int $parentId): array
- {
- $result = [];
- foreach ($this->records as $record) {
- if ($record['parent_id'] === $parentId) {
- $record['children'] = $this->build($record['id']);
- $result[] = $record;
- }
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement