通过引用取消设置数组的元素 [英] unset a element of an array via reference

查看:73
本文介绍了通过引用取消设置数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过引用方法访问多维数组内的任何地方。而且我可以更改其值。例如:

I can access anywhere inside the multi-dimensional an array via reference method. And I can change the its value. For example:

$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'
    )
);

但是,我无法销毁该部分:

BUT, I can't destroy the that section:

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

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

有解决方案吗?

推荐答案

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

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;

ie

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

unset  $value['host'];

这篇关于通过引用取消设置数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆