Advertisement
qsadfasdgfgads

Untitled

Mar 31st, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.55 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace App\Services;
  5.  
  6.  
  7. class Tree
  8. {
  9.     /**
  10.      * @var array
  11.      */
  12.     private $records;
  13.  
  14.     public function __construct(array $records)
  15.     {
  16.         $this->records = $records;
  17.     }
  18.  
  19.     public function build(int $parentId): array
  20.     {
  21.         $result = [];
  22.         foreach ($this->records as $record) {
  23.             if ($record['parent_id'] === $parentId) {
  24.                 $record['children'] = $this->build($record['id']);
  25.                 $result[] = $record;
  26.             }
  27.         }
  28.         return $result;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement