我遍历一个对象数组,并希望根据其"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相关问答推荐

为什么这个方法调用用引号和花括号包裹?

在WooCommerce产品档案页面中显示产品销售百分比

为什么调用干预\映像引发错误驱动程序\GD\Driver not found?

致命错误:无法使用简单的php博客重新声明spl_autoload_Register()

在WooCommerce中禁用特定日期之前的免费送货

在WooCommerce产品变体SKU字段旁边添加自定义输入字段

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

如何在execute语句中使用存储过程参数?

服务器升级到新的mysql版本8.0.34后查询错误

API 平台 - UI 服务器端点定义

奇怪的 preg_match_all() 行为

如何在 PHP laravel 中将图像的透明背景转换为白色?

在特征中使用类的属性

如何获得没有运输等级的产品

在WooCommerce可变产品中显示所选变体的自定义字段

Xdebug 不会在断点处停止

使用 Process facade 在外部 Laravel 项目上运行 Artisan 命令会返回主项目的数据库错误

在 laravel 中更新表单时如何在 Select 选项中保留旧值?

我需要获取传递到这个数组中的数据,但我没有得到它,因为在 Laravel 中数组是这样出现的

WooCommerce 按 ID 限制产品