123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace app\helpers;
- use yii\helpers\ArrayHelper;
- class Tree
- {
- public static function buildTree(array $elements, $parentId = 0) {
- $branch = array();
- foreach ($elements as $element) {
- if ($element['parent_id'] == $parentId) {
- $children =self::buildTree($elements, $element['id']);
- if ($children) {
- $element['children'] = $children;
- }
- $branch[] = $element;
- }
- }
- return $branch;
- }
- public static function drawTree( $items, $draw, $lavel = 0 ){
- foreach( $items as $item ){
- echo $draw->render('_item', [
- 'model' => $item['item'], 'lavel' => $lavel
- ]);
- if( isset( $item['children'] ) && $item['children'] ){
- self::drawTree( $item['children'], $draw, $lavel+1 );
- }
- }
- }
- }
|