加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:16:46 所属栏目:PHP教程 来源:网络整理
导读:我可以通过引用方法访问多维数组中的任何位置.我可以改变它的价值.例如: $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'
我可以通过引用方法访问多维数组中的任何位置.我可以改变它的价值.例如:
$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','host' => '-- changed ---'
            'port' => '3306'
    )
);

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

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

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

有解决方案吗?

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

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

unset  $value['host'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读