douyuan4357 2014-11-10 10:13
浏览 20
已采纳

删除数组PHP的某些部分

I need to remove a certain key of my array since I'm creating a filter for my data.

Array(
   [0]=>Array
      (
        ['Column1'] => 'ABC'
        ['Column2'] => 'xxx'
      )
   [1]=>Array
      (
        ['Column1'] => 'XYZ'
        ['Column2'] => 'xxx'
      )
)

I want to remove the key (meaning the number 2) that has the value 'XYZ'. How can I remove it? I need to remove it because I am filtering the array that was thrown to me by another script and I need to remove the key. I tried using for loop but I do not know how to remove it.

for($z = 0; $z < count($array);$z++)
{
   if($array[$z]['Column1'] == 'XYZ'){
          // how do I remove the record [1] and all of its contents?
  }
}
  • 写回答

2条回答 默认 最新

  • douzhang5295 2014-11-10 10:15
    关注

    Use unset()

    for($z = 0; $z < count($array);$z++)
    {
       if($array[$z]['Column1'] == 'XYZ'){
         unset($array[$z]);
      }
    }
    

    You can also do:

    foreach($array as &$v) {
      if($v['Column1'] == 'XYZ') {
        unset($v);
      }
    }
    

    After using unset() on an array, and as long as you don't need to retain the index values, it is worth doing:

    $array = array_values($array);
    

    To reset the array index.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了