你如何删除在foreach循环数组元素? [英] How do you remove an array element in a foreach loop?
本文介绍了你如何删除在foreach循环数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要遍历与的foreach
一个数组,以检查是否存在一个值。如果值确实存在,我想删除其中包含它的元素。
I want to loop through an array with foreach
to check if a value exists. If the value does exist, I want to delete the element which contains it.
我有以下的code:
foreach($display_related_tags as $tag_name) {
if($tag_name == $found_tag['name']) {
// Delete element
}
}
我不知道如何,一旦价值被发现删除的元素。如何删除呢?
I don't know how to delete the element once the value is found. How do I delete it?
我必须使用的foreach
这个问题。有可能替代的foreach
,欢迎你来分享。
I have to use foreach
for this problem. There are probably alternatives to foreach
, and you are welcome to share them.
推荐答案
如果您还拿到钥匙,就可以删除像这样的项目:
If you also get the key, you can delete that item like this:
foreach ($display_related_tags as $key => $tag_name) {
if($tag_name == $found_tag['name']) {
unset($display_related_tags[$key]);
}
}
这篇关于你如何删除在foreach循环数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文