gpt4 book ai didi

php - 如何从foreach循环内的数组中删除对象?

转载 作者:IT老高 更新时间:2023-10-28 11:45:52 24 4
gpt4 key购买 nike

我遍历一个对象数组并想根据它的 '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
}
}
}

任何建议。谢谢。

最佳答案

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]);
}
}
}

关于php - 如何从foreach循环内的数组中删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2304570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com