安全删除foreach中的数组元素 [英] safe to delete elements of array in foreach

查看:84
本文介绍了安全删除foreach中的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从foreach正在处理的数组中删除元素是否安全?

(通常情况并非如此,但在php中不确定)如果有,那么

有效的方式来处理它? (我可以将索引添加到临时数组中,然后在必要时将
删除,但由于我实际上在嵌套的

情况下工作,这可能会有点混乱。我想我可以将值设置为

null并在之后删除它们?


谢谢,

Jon

Is it safe to remove elements from an array that foreach is working on?
(normally this is not the case but not sure in php) If so is there an
efficient way to handle it? (I could add the indexes to a temp array and
delete afterwards if necessary but since I''m actually working in a nested
situation this could get a little messy. I guess I could set there values to
null and remove them afterwards?

Thanks,
Jon

推荐答案

5月18日上午11:05,Jon Slaughter< Jon_Slaugh ... @ Hotmail.com>

写道:
On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:

从foreach正在处理的数组中删除元素是否安全?

(通常情况并非如此但不确定在PHP)如果是这样有一个

有效的方法来处理它?(我可以添加索引到临时数组和

删除后,如果有必要,但因为我''我实际上是在嵌套的

情况下工作,这可能会有点混乱。我想我可以将值设置为

null并在之后删除它们?


谢谢,

Jon
Is it safe to remove elements from an array that foreach is working on?
(normally this is not the case but not sure in php) If so is there an
efficient way to handle it? (I could add the indexes to a temp array and
delete afterwards if necessary but since I''m actually working in a nested
situation this could get a little messy. I guess I could set there values to
null and remove them afterwards?

Thanks,
Jon



为什么不试试看看会发生什么?

Why don''t you try it and see what happens?




" ZeldorBlat" < ze ******** @ gmail.com写信息

新闻:11 ********************* @ p77g2000hsh.googlegro ups.com ...

"ZeldorBlat" <ze********@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...

5月18日上午11:05,Jon Slaughter < Jon_Slaugh ... @ Hotmail.com>

写道:
On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:

>从数组中删除元素是否安全正在研究?
(通常情况并非如此,但在php中不确定)如果是这样有效的方法来处理它? (我可以将索引添加到临时数组中,然后在必要时删除
但是因为我实际上在嵌套的情况下工作,这可能会有点混乱。我想我可以设置值<是否
null并在之后删除它们?

谢谢,
Jon
>Is it safe to remove elements from an array that foreach is working on?
(normally this is not the case but not sure in php) If so is there an
efficient way to handle it? (I could add the indexes to a temp array and
delete afterwards if necessary but since I''m actually working in a nested
situation this could get a little messy. I guess I could set there values
to
null and remove them afterwards?

Thanks,
Jon



为什么不你试试看看会发生什么?


Why don''t you try it and see what happens?



嗯...因为我做了......但这并不意味着什么。只是让某人尝试

有些东西并不能证明它会一直这样......


有更多好主意吗?


或者难的问题是什么?

Um... cause I did... but that doesn''t mean much. Just cause someone tries
something doesn''t prove that it will always work like that...

got any more bright ideas?

Or is the question to hard for you?


5月18日上午11点40分,Jon Slaughter< Jon_Slaugh ... @ Hotmail.com>

写道:
On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:

" ZeldorBlat"< zeldorb ... @ gmail.comwrote in message


news:11 ********************* @ p77g2000hsh.googlegro ups.com ......
"ZeldorBlat" <zeldorb...@gmail.comwrote in message

news:11*********************@p77g2000hsh.googlegro ups.com...

5月18日上午11:05,Jon Slaughter < Jon_Slaugh ... @ Hotmail.com>

写道:
On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:

从foreach工作的数组中删除元素是否安全在?

(通常这不是这种情况,但在php中不确定)如果是这样有一个

有效的方法来处理它? (我可以将索引添加到临时数组中,然后在必要时将
删除,但由于我实际上在嵌套的

情况下工作,这可能会有点混乱。我想我可以设置值



null并在之后删除它们?
Is it safe to remove elements from an array that foreach is working on?
(normally this is not the case but not sure in php) If so is there an
efficient way to handle it? (I could add the indexes to a temp array and
delete afterwards if necessary but since I''m actually working in a nested
situation this could get a little messy. I guess I could set there values
to
null and remove them afterwards?


谢谢,

Jon
Thanks,
Jon


为什么要'你试试看看会发生什么事吗?
Why don''t you try it and see what happens?



嗯...因为我做了......但这并不意味着什么。只是让某人尝试

有些东西并不能证明它会一直这样......


有更多好主意吗?


或者对你来说难的问题是什么?


Um... cause I did... but that doesn''t mean much. Just cause someone tries
something doesn''t prove that it will always work like that...

got any more bright ideas?

Or is the question to hard for you?



不,问题不在于(原文如此)对我来说很难。但是,正如你已经

被发现,测试也不困难。

No, the question is not to (sic) hard for me. But, as you''ve already
discovered, it isn''t that difficult to test, either.


这篇关于安全删除foreach中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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