通过引用取消设置数组的元素

ebu*_*han 11 php arrays reference multidimensional-array unset

我可以通过引用方法访问多维数组中的任何位置.我可以改变它的价值.例如:

$conf = array(
    'type' => 'mysql',
    'conf' => array(
            'name' => 'mydatabase',
            'user' => 'root',
            'pass' => '12345',
            'host' => array(
                    '127.0.0.1',
                    '88.67.45.123',
                    '129.34.123.55'
            ),
            'port' => '3306'
    )
);

$value = & $this->getFromArray('type.conf.host');
$value = '-- changed ---';

// result
$conf = array(
    'type' => 'mysql',
    'conf' => array(
            'name' => 'mydatabase',
            'user' => 'root',
            'pass' => '12345',
            'host' => '-- changed ---'
            'port' => '3306'
    )
);
Run Code Online (Sandbox Code Playgroud)

但是,我不能破坏该部分:

// normally success
unset($conf['type']['conf']['host']);

// fail via reference
$value = & $this->getFromArray('type.conf.host');
unset($value);
Run Code Online (Sandbox Code Playgroud)

有解决方案吗?

Zak*_*Zak 6

好吧,我认为更好的答案.为了取消设置,你应该获得对容器数组的引用,然后取消设置数组中的元素;

$value = & $this->getFromArray('type.conf');

unset  $value['host'];
Run Code Online (Sandbox Code Playgroud)