duanjie2701 2013-10-29 08:55
浏览 61
已采纳

删除数组子项,其中value =多维数组中的某个值

I have a multidimentional array like this $membergroups :

Array ( [0] => 
    Array ( [id] => 1645819602 
        [name] => Oryza NurFa 
        [first_name] => Oryza 
        [last_name] => NurFa 
        [work] => MAN 2 Yogyakarta ) 
    [1] => 
    Array ( [id] => 100000251643877 
        [name] => Lathif Pambudi 
        [first_name] => Muhammad Lathif 
        [last_name] => Pambudi 
        [work] => Omah TI ) 
    [2] => 
    Array ( [id] => 1152078197 
        [name] => Novantio Bangun 
        [first_name] => Novantio 
        [last_name] => Bangun 
        [work] => Pertamina))

How to delete one of child array with specific value. For the example, I want to delete an array with [id] => 100000251643877 inside? So the output will be :

Array ( [0] => 
        Array ( [id] => 1645819602 
            [name] => Oryza NurFa 
            [first_name] => Oryza 
            [last_name] => NurFa 
            [work] => MAN 2 Yogyakarta ) 
        [1] => 
        Array ( [id] => 1152078197 
            [name] => Novantio Bangun 
            [first_name] => Novantio 
            [last_name] => Bangun 
            [work] => Pertamina))

Here is my php code, but it doesn't work :

if (($key = array_search($user_fbid, $membergroups)) !== false) {
   unset($membergroups[$key]);
}

Any help would be greatly appreciated. Thank you

  • 写回答

4条回答 默认 最新

  • dongzheng4556 2013-10-29 09:00
    关注

    Using a foreach you can do the job like this

    $id = 100000251643877;//Example
    foreach($membergroups as $key => $value){
      if($value['id'] == $id){
        unset($membergroups[$key]);  
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)