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;
}