取消设置 php 引用
所以我有这个函数,它返回对传入数组的特定点的引用。我想调用 unset ,然后从数组/引用中删除结果,但调用 unset 仅删除引用,不是原始数组中的数据。有什么想法吗?
So I have this function, and it returns me a reference to a particular point to the array passed in. I want to make a call to unset that will then remove the result from the array/reference, but calling unset only removes the reference, not the data from the original array. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将引用设置为
null
将破坏该引用(以及任何其他引用)链接到的数据。有关详细信息,请参阅手册中的取消设置引用。基本上你想要执行以下操作(摘自评论):
在你的情况下,它看起来像这样:
编辑
为了消除任何误解,以下是取消设置数组引用时得到的结果:
请注意,在第一个示例中,第二个数组索引并未使用
unset()
删除其内容,但第二个示例将引用设置为null
来完成此操作。注意:如果您还需要取消设置数组索引(我不太清楚您是否这样做),那么您需要找到一种方法来引用数组的键而不是值,可能是通过改变函数的返回值来实现的。
Setting the reference to
null
will destroy the data that the reference (and any other reference) is linked to.See Unsetting References in the manual for more on this. Basically you want to do the following (taken from the comments):
In your case, it'll look something like this:
EDIT
To clear up any misconceptions, here's what you get when you unset array references:
Notice how the second array index does not have it's content deleted in the first example with
unset()
, but the second example of setting the reference tonull
accomplishes this.Note: If you need to unset the array index as well, which I'm a bit unclear on as to whether you do or not, then you'll need to find a way to reference the key of the array instead of the value, likely by altering the return value of your function.
取消设置引用不会取消设置所引用的变量,这是预期的行为。一种解决方案是返回键而不是值,并使用它来取消设置原始值。
It is expected behavior that unsetting a reference does not unset the variable being referenced. One solution is to return the key instead of the value, and using that to unset the original value.
请注意,
unset
对引用的行为< /a> 是设计使然。您可以返回要删除的元素的索引,或者返回索引数组(如果数组不平坦)。例如,您可以使用以下函数:
或者,如果您更喜欢异常,
Note that the behavior of
unset
on references is by design. You could instead return the index of the element to remove, or an array of indices if the array isn't flat.For example, you could use the following function:
Or, if you prefer exceptions,