php 如何从foreach循环内的数组中删除对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2304570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete object from array inside foreach loop?
提问by ababa
I iterate through an array of objects and want to delete one of the objects based on it's 'id' property, but my code doesn't work.
我遍历一个对象数组,并想根据它的 'id' 属性删除其中一个对象,但我的代码不起作用。
foreach($array as $element) {
foreach($element as $key => $value) {
if($key == 'id' && $value == 'searched_value'){
//delete this particular object from the $array
unset($element);//this doesn't work
unset($array,$element);//neither does this
}
}
}
Any suggestions. Thanks.
有什么建议。谢谢。
回答by prodigitalson
foreach($array as $elementKey => $element) {
foreach($element as $valueKey => $value) {
if($valueKey == 'id' && $value == 'searched_value'){
//delete this particular object from the $array
unset($array[$elementKey]);
}
}
}
回答by pablo.meier
It looks like your syntax for unset is invalid, and the lack of reindexing might cause trouble in the future. See: the section on PHP arrays.
看起来您的 unset 语法无效,并且缺乏重新索引可能会在将来造成麻烦。请参阅: 有关 PHP 数组的部分。
The correct syntax is shown above. Also keep in mind array-valuesfor reindexing, so you don't ever index something you previously deleted.
正确的语法如上所示。还要记住用于重新索引的数组值,因此您永远不会索引之前删除的内容。
回答by air-dex
You can also use references on foreachvalues:
您还可以对foreach值使用引用:
foreach($array as $elementKey => &$element) {
// $element is the same than &$array[$elementKey]
if (isset($element['id']) and $element['id'] == 'searched_value') {
unset($element);
}
}
回答by magallanes
Be careful with the main answer.
请注意主要答案。
with
和
[['id'=>1,'cat'=>'vip']
,['id'=>2,'cat'=>'vip']
,['id'=>3,'cat'=>'normal']
and calling the function
并调用函数
foreach($array as $elementKey => $element) {
foreach($element as $valueKey => $value) {
if($valueKey == 'cat' && $value == 'vip'){
//delete this particular object from the $array
unset($array[$elementKey]);
}
}
}
it returns
它返回
[2=>['id'=>3,'cat'=>'normal']
instead of
代替
[0=>['id'=>3,'cat'=>'normal']
It is because unset does not re-index the array.
这是因为 unset 不会重新索引数组。
It reindexes. (if we need it)
它重新索引。(如果我们需要的话)
$result=[];
foreach($array as $elementKey => $element) {
foreach($element as $valueKey => $value) {
$found=false;
if($valueKey === 'cat' && $value === 'vip'){
$found=true;
$break;
}
if(!$found) {
$result[]=$element;
}
}
}
回答by Josh
This should do the trick.....
这应该可以解决问题......
reset($array);
while (list($elementKey, $element) = each($array)) {
while (list($key, $value2) = each($element)) {
if($key == 'id' && $value == 'searched_value') {
unset($array[$elementKey]);
}
}
}
回答by Corey Sunwold
I'm not much of a php programmer, but I can say that in C# you cannot modify an array while iterating through it. You may want to try using your foreach loop to identify the index of the element, or elements to remove, then delete the elements after the loop.
我不是一个 php 程序员,但我可以说在 C# 中你不能在迭代数组时修改它。您可能想尝试使用 foreach 循环来标识元素的索引或要删除的元素,然后在循环后删除元素。

