1

我有一个像这样的多维数组$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))

如何删除具有特定值的子数组之一。例如,我想删除一个[id] => 100000251643877里面的数组?所以输出将是:

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))

这是我的php代码,但它不起作用:

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

任何帮助将不胜感激。谢谢

4

4 回答 4

4

您可以使用array_column但仅限于php >= 5.5

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

array_column( $membergroups, 'id')在 membergroup 多维数组中搜索 id 列,并向您返回一个包含所有行值条目的数组,其中包含 id 键。

数组列->MANUAL

于 2013-10-29T08:57:53.680 回答
0

循环遍历整个数组:

foreach ($membergroups as $idx => $group) {
  if ($group['id'] === $user_fbid) {
    unset($membergrouops[$idx]);
    break;
  }
}
于 2013-10-29T08:59:42.177 回答
0

使用 foreach 你可以做这样的工作

$id = 100000251643877;//Example
foreach($membergroups as $key => $value){
  if($value['id'] == $id){
    unset($membergroups[$key]);  
  }
}
于 2013-10-29T09:00:54.143 回答
0
foreach($membergroups as $key => $value){
  if($value['id'] == $user_fbid){
   unset($membergroups[$key]);  
  }
}

不要忘记在删除键后合并数组以保持索引连续

$membergroups = array_merge($membergroups);
于 2013-10-31T03:03:37.200 回答