core
core copied to clipboard
about set and get the some update code
public function find(&$data, $key = null, $value = null, $force = false) { $link = explode('.', $key); foreach ($link as $keys => $part) { if (isset($data[$part])) { if ($force) { if ($keys + 1 === count($link)) { $data[$part] = $value; } else { $data = &$data[$part]; } } else { $data = &$data[$part]; } } else { if ($force) { if ($keys + 1 === count($link)) { $data[$part] = $value; } else { $data[$part] = array(); $data = &$data[$part]; } } else if ($part) { return null; } } } return $data; }
public function get($key = null)
{
if ($key === null) {
return $this->vars;
}
if (is_array($key) || is_object($key)) {
$vars = [];
foreach ($key as $k) {
$vars[$k] = $this->find($this->vars, $k);
}
return $vars;
} else {
return $this->find($this->vars, $key);
}
}
public function set($key, $value = null)
{
if (is_array($key) || is_object($key)) {
foreach ($key as $k => $v) {
$this->find($this->vars, $k, $v, true);
}
} else {
$this->find($this->vars, $key, $value, true);
}
}
public function has($key)
{
return $this->find($this->vars, $key) != null;
}
public function clear($key = null)
{
if (is_null($key)) {
$this->vars = array();
} else {
$link = explode('.', $key);
$data = &$this->vars;
foreach ($link as $part) {
if (isset($data[$part])) {
$last = &$data;
$data = &$data[$part];
} else {
return false;
}
}
if ($last) {
unset($last[$part]);
}
}
}
Sorry, forgive me for my poor English