简体   繁体   English

如何取消设置对象数组中所有对象的键?

[英]How to unset a key in all objects in an array of objects?

I'm trying to unset a key in all objects in an array of objects (basically removing any passwords), doing this:我试图在对象数组中的所有对象中取消设置一个键(基本上是删除任何密码),这样做:

    foreach ( $data['users'] as $user) {
        unset($user['password']);
    }

But it seems it doesn't effect the 'original' data ... how do I do this by reference (or whatever it takes to make this work as 'expected' – by which I mean, the key is removed from all objects in the original array)?但它似乎不会影响“原始”数据......我如何通过引用来做到这一点(或使这项工作按“预期”进行的任何事情 - 我的意思是,从所有对象中删除密钥原始数组)?

Try:尝试:

foreach ( $data['users'] as $key => $user) {
    unset($data['users'][$key]['password']);
}

Or或者

foreach ( $data['users'] as &$user) {
    unset($user['password']);
}

You can pass the $user as reference like this :您可以像这样传递$user作为参考:

// check this --------------v
foreach ( $data['users'] as &$user) {
    unset($user['password']);
}

我的建议:

array_walk($data['users'], function(&$a) {unset($a['password']);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM