Заполнить дерево объектов сущностями.

@param array $tree
@param $nodes

@return mixed
549912
protected function fillTreeNodesForItems($tree, $nodes)
    {
        foreach ($tree as $key => $item) {
            $tree[$key]['node'] = $nodes[$item['id']] ?? null;

            $tree[$key]['__children'] = $this->fillTreeNodesForItems($tree[$key]['__children'], $nodes);
        }

        return $tree;
    }
15943
public static function createFromArray(array $tree, EntityInterface $parent = null)
    {
        $childrenRelationIndex = with(new static)->getChildrenRelationIndex();
        $entities = [];

        foreach ($tree as $item) {
            $children = array_pull($item, $childrenRelationIndex);

            /**
             * @var Entity $entity
             */
            $entity = new static($item);
            $entity->parent_id = $parent ? $parent->getKey() : null;
            $entity->save();

            if (!is_null($children)) {
                $children = static::createFromArray($children, $entity);
                $entity->setRelation($childrenRelationIndex, $children);
                $entity->addChildren($children->all());
            }

            $entities[] = $entity;
        }

        return new Collection($entities);
    }
1038373
public static function tree_to($tree, $fchildrens = 'children')
    {
        $arr = [];
        if (isset($tree[$fchildrens]) && is_array($tree[$fchildrens])) {
            foreach ($tree[$fchildrens] as $child) {
                $arr = array_merge($arr, self::tree_to($child, $fchildrens));
            }
            unset($tree[$fchildrens]);
            $arr[] = $tree;
        } else {
            $arr[] = $tree;
        }
        return $arr;
    }
1032202
protected function mergeTrees(array $trees): array
    {
        if (count($trees) == 0) {
            return [];
        }

        $tree1 = array_shift($trees);
        while (count($trees)) {
            $tree2 = array_shift($trees);
            foreach ($tree2 as $key => $value) {
                if (in_array($key, $this->permissionKeys)) {
                    $tree1 = $tree2;
                    break;
                }
                if (isset($tree1[$key]) && is_array($value)) {
                    $tree1[$key] = $this->mergeTrees([$tree1[$key], $tree2[$key]]);
                    continue;
                }
                $tree1[$key] = $value;
            }
        }

        return $tree1;
    }
352548
public function findBranchByNodeId(Branch $tree, $nodeId)
    {
        foreach ($tree->getChildren() as $branch) {
            if ($branch->getNodeId() == $nodeId) {
                return $branch;
            }

            // recursive search:
            $branch = $this->findBranchByNodeId($branch, $nodeId);
            if ($branch) {
                return $branch;
            }
        }

        return null;
    }
629976
protected function &getNode(&$node, $key)
    {
        if (is_object($node) && isset($node->$key)) {
            return $node->$key;
        }

        if (is_array($node) && isset($node[$key])) {
            return $node[$key];
        }

        // Special handling of eloquent relations
        if (!$this->isEloquentModel($node)) {
            $result = null;

            return $result;
        }

        if (!method_exists($node, $key)) {
            $result = null;

            return $result;
        }

        $relation = $node->{$key}();

        if (!$this->isEloquentRelation($relation)) {
            $result = null;

            return $result;
        }

        return $relation->getResults();
    }
435724
protected function addPermissions($tree)
    {
        $user = $this->getSession()->getUser();

        $section = MenuService::getSection(
            $this->getRequestParameter('extension'),
            $this->getRequestParameter('perspective'),
            $this->getRequestParameter('section')
        );

        $actions = $section->getActions();

        //then compute ACL for each node of the tree
        $treeKeys = array_keys($tree);
        if (isset($treeKeys[0]) && is_int($treeKeys[0])) {
            foreach ($tree as $index => $treeNode) {
                $tree[$index] = $this->computePermissions($actions, $user, $treeNode);
            }
        } else {
            $tree = $this->computePermissions($actions, $user, $tree);
        }

        return $tree;
    }
948965
protected function buildTree(array $simpleTree, $tree = [])
    {
        foreach ($simpleTree as $item) {
            if (!isset($this->collection[$item['id']])) {
                continue;
            }

            $content = $this->collection[$item['id']];

            unset($this->collection[$item['id']]); // TODO Fix multi-usage of single item

            if (isset($item['__children']) && count($item['__children'])) {
                $content['__children'] = $this->buildTree($item['__children']);
            } else {
                $content['__children'] = [];
            }

            $tree[] = $content;
        }

        return $tree;
    }
425213
public function marshall(array $tree = array())
    {
        $data = array();

        $private_properties = array(
            'row_wrapper_class',
            'index_field',
            'options'
        );

        foreach ($private_properties as $property) {
            $data[$property] = $this->$property;
        }

        $data['objects'] = array();

        foreach ($this->objects as $object) {
            if ($object instanceof SwatDBMarshallable) {
                $object_data = $object->marshall($tree);
                $data['objects'][] = $object_data;
            }
        }

        return $data;
    }
1210518
public function getNodeName($tree)
    {
        if ($models = ArrayHelper::getValue($this->treeWidgetOptions, 'models')) {
            $model = $models[0];
            $rootLevel = $model->level;

            /**
             * @var \skeeks\cms\models\CmsTree $tree
             */
            $name = $tree->name;
            if ($tree->parents) {
                $parents = $tree->getParents()->andWhere(['>=', 'level', $rootLevel])->all();
                if ($parents) {
                    $name = implode(" / ", \yii\helpers\ArrayHelper::map($parents, 'id', 'name'));
                    $name .= " / " . $tree->name;
                }
            }

            return $name;
        } else {
            $rootLevel = 0;

            /**
             * @var \skeeks\cms\models\CmsTree $tree
             */
            $name = $tree->name;
            if ($tree->parents) {
                $parents = $tree->getParents()->andWhere(['>=', 'level', $rootLevel])->all();
                if ($parents) {
                    $name = implode(" / ", \yii\helpers\ArrayHelper::map($parents, 'id', 'name'));
                    $name .= " / " . $tree->name;
                }
            }

            return $name;
        }

        return $tree->name;
    }
1008434
private function findBegin(array &$tree): int {
        $base  = 1;
        $found      = false;
        while (!$found) {
            // 步进
            $base++;

            // 如有使用跳过
            if (isset($this->begin_used[$base])) {
                continue;
            }

            // 查找是否符合条件
            foreach ($tree as $child_code => $child_tree) {
                if (isset($this->check[$this->getState($base, $child_code)])) {
                    continue 2;
                }
            }

            $found = true;
        }
        return $base;
    }
1334303
public function mapTree(array $nodes, array $mappedNodes = []) {
        if (!$mappedNodes) {
            return $nodes;
        }

        $tree = [];
        $pk = $this->getRepository()->getPrimaryKey();

        foreach ($nodes as $node) {
            $id = $node[$pk];

            if (isset($mappedNodes[$id])) {
                $node[$this->getConfig('treeField') ?: 'Nodes'] = $this->mapTree($mappedNodes[$id], $mappedNodes);
            }

            $tree[] = $node;
        }

        return $tree;
    }
274764
private function prepareNodesTree(
        $nodes,
        $webspaceKey,
        $languageCode,
        $complete = true,
        $excludeGhosts = false,
        $maxDepth = 1,
        $currentDepth = 0
    ) {
        ++$currentDepth;

        $results = [];
        foreach ($nodes as $node) {
            $result = $this->prepareNode($node, $webspaceKey, $languageCode, 1, $complete, $excludeGhosts);
            if (null !== $maxDepth &&
                $currentDepth < $maxDepth &&
                $node->getHasChildren() &&
                null != $node->getChildren()
            ) {
                $result['_embedded']['nodes'] = $this->prepareNodesTree(
                    $node->getChildren(),
                    $webspaceKey,
                    $languageCode,
                    $complete,
                    $excludeGhosts,
                    $maxDepth,
                    $currentDepth
                );
            }
            $results[] = $result;
        }

        return $results;
    }
894139
protected function saveTreeToCache(array $tree)
    {
        $cachedTree = $this->cache->getItem('ordermind.logauth.permissions');
        $cachedTree->set($tree);
        $this->cache->save($cachedTree);
    }
761218
public function getMenu(Tree $tree): ?Menu
    {
        if (Tree::all()->count() === 1 || Site::getPreference('ALLOW_CHANGE_GEDCOM') !== '1') {
            return new Menu(I18N::translate('Family tree'), route('tree-page', ['ged' => $tree->name()]), 'menu-tree');
        }

        $submenus = [];
        foreach (Tree::all() as $menu_tree) {
            if ($menu_tree->id() === $tree->id()) {
                $active = 'active ';
            } else {
                $active = '';
            }
            $submenus[] = new Menu(e($menu_tree->title()), route('tree-page', ['ged' => $menu_tree->name()]), $active . 'menu-tree-' . $menu_tree->id());
        }

        return new Menu(I18N::translate('Family trees'), '#', 'menu-tree', [], $submenus);
    }
1084190
public function toString(Array $tree) {
    $text = '';
    while (!empty($tree)) {
      $node = array_shift($tree);

      $text .= str_repeat('  ',$node->getDepth()).(string) $node."\n";
    }
    
    return $text;
  }
1017940
protected function nodeToDictionary( Node $node )
    {
        $result = $node->attributes;

        $containerKeys = [];

        foreach ($node->children as $child) {
            $arrayChild = $this->nodeToDictionary($child);

            $childName = $child->name;
            $childValue = $arrayChild;

            if (sizeof($child->children) == 0) {
                $childValue = $child->text;
            }

            if (isset( $result[ $childName ] )) {
                if (!in_array($childName, $containerKeys)) {
                    $result[ $childName ] = [$result[ $childName ]];
                    $containerKeys[] = $childName;
                }

                $result[ $childName ][] = $childValue;
            } else {
                $result[ $childName ] = $childValue;
            }
        }

        return $result;
    }