phprs\util\Tree::insert PHP Method

insert() public method

插入一个节点 插入时允许使用通配符*
public insert ( array $path, unknown $value, string $replace_exits = false, mixed &$replaced = null ) : boolean
$path array
$value unknown
$replace_exits string 是否替换已存在的
$replaced mixed 被替换的原始值
return boolean
    public function insert($path, $value, $replace_exits = false, &$replaced = null)
    {
        assert(count($path));
        $left = $path;
        $cur = array_shift($left);
        //剩余未遍历的
        if (!array_key_exists($cur, $this->arr)) {
            $this->arr[$cur] = array();
        }
        $insert = false;
        $this->visitNode($path, function ($name, &$node) use(&$insert, &$left, $value, $replace_exits, &$replaced) {
            $cur = array_shift($left);
            if ($cur === null) {
                if (array_key_exists('value', $node)) {
                    //是否已存在
                    if (!$replace_exits) {
                        return false;
                    }
                    $replaced = $node['value'];
                }
                $node['value'] = $value;
                $insert = true;
                return false;
                //停止遍历
            } else {
                if (!array_key_exists('next', $node)) {
                    $node['next'] = array();
                }
                if (!array_key_exists($cur, $node['next'])) {
                    $node['next'][$cur] = array();
                }
            }
            return true;
        }, true);
        return $insert;
    }