unset a element of an array via reference
Ok, better answer I think. In order to unset , you should get a reference to the container array, then unset the element within the array;
i.e.
$value = & $this->getFromArray('type.conf');unset $value['host'];
References are not like hard-links. If you unset a reference, this will not unset the origin value.
<?php $a = 5;xdebug_debug_zval('a'); // a: (refcount=1, is_ref=0), int 5$b = &$a;xdebug_debug_zval('a'); // a: (refcount=2, is_ref=1), int 5xdebug_debug_zval('b'); // b: (refcount=2, is_ref=1), int 5unset($b);xdebug_debug_zval('a'); // a: (refcount=1, is_ref=0), int 5
Why not write a little Config
class which abstracts the data (array)? Since objects are always passed by reference you won't need to handle this at your own.
class Config{ // ...}$config = new Config(array( 'db' => array( 'name' => 'mydatabase', 'user' => 'root', 'pass' => '12345', )));$config->get('db.user');$config->set('db.user', 'newuser');$config->unset('db.user');//...
Here is my function for unsetting nested keys
public function unsetKey(string $dotSeparatedKey){ $keys = explode('.', $dotSeparatedKey); $pointer = &$this->data; $current = false; // just to make code sniffer happy // we traverse all but the last key while (($current = array_shift($keys)) && (count($keys) > 0)) { // if some key is missing all the subkeys will be already unset if (!array_key_exists($current, $pointer)) { // is already unset somewhere along the way return; } // set pointer to new, deeper level // called for all but last key $pointer = &$pointer[$current]; } // handles empty input string if ($current) { // we finally unset what we wanted unset($pointer[$current]); }}