src/AppBundle/Common/Tree.php

(开头部分) 1KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

<?php

namespace AppBundle\Common;

/**
 * Class Tree 多叉树 数据结构类
 * 对树的操作是递归的,如有特殊需求导致递归会爆栈 可以改用stack来实现递归的效果.
 */
class Tree
{
    /**
     * @var array<Tree>
     */
    private $children = array();

    /**
     * @var Tree
     */
    private $parent;

    /**
     * @var mixed
     */
    public $data;

    public function __construct($data = array(), Tree $parent = null)
    {
        $this->data = $data;
        $this->parent = $parent;
    }

    /**
     * Like ArrayToolkit::column.
     *
     * @param $key
     *
     * @return mixed
     */
    public function column($key)
    {
        return $this->reduce(function ($ret, $tree) use ($key) {
            if (!empty($tree->data[$key])) {
                array_push($ret, $tree->data[$key]);
            }

            return $ret;
        }, array());
    }

    /**
     * each Tree Node call closure.
     *
     * @param \Closure $closure
     *
     * @return $this
     */
    public function each(\Closure $closure)
    {
        $closure($this);
        foreach ($this->getChildren() as $child) {

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。