如何从php中删除2D数组中的元素

时间:2013-03-29 07:24:50

标签: php arrays

我的阵列看起来像吼叫

Array
(
    [0] => Array
        (
            [REDUNDANT] => NO
            [FILE_NAME] => 51894a (2).pdf
        )
    [1] => Array
        (
            [REDUNDANT] => YES
            [FILE_NAME] => book (2).pdf
        )
    [2] => Array
        (
            [REDUNDANT] => YES
            [FILE_NAME] => samae (1).pdf
        )
    [3] => Array
        (
            [REDUNDANT] => NO
            [FILE_NAME] => aswss (1).pdf
        )        
)

我想删除array[INDEX][REDUNDANT] == 'YES'

的所有元素

删除数组后,新数组如下所示

Array
(
    [0] => Array
        (
            [REDUNDANT] => NO
            [FILE_NAME] => 51894a (2).pdf
        )
    [1] => Array
        (
            [REDUNDANT] => NO
            [FILE_NAME] => aswss (1).pdf
        )        
)

请帮助我找到解决方案,谢谢

3 个答案:

答案 0 :(得分:1)

试试这个,

foreach($files as $key => $file){
   if($file['REDUNDANT'] == "Yes"){
       unset($files[$key]);
   }
}
$files = array_values($files); // Reset key of updated array.

Codepad Demo.

答案 1 :(得分:1)

试试这个

   $abc= Array
   (
        [0] => Array
        (
              [REDUNDANT] => NO
            [FILE_NAME] => 51894a (2).pdf
            )
      [1] => Array
    (
        [REDUNDANT] => YES
        [FILE_NAME] => book (2).pdf
    )
[2] => Array
    (
        [REDUNDANT] => YES
        [FILE_NAME] => samae (1).pdf
    )
[3] => Array
    (
        [REDUNDANT] => NO
        [FILE_NAME] => aswss (1).pdf
    )        
)

      foreach($abc as $subKey => $subArray){
      if($subArray['REDUNDANT'] =='yes'){
           unset($abc[$subKey]);
        }
     }

   print_r($abc);

答案 2 :(得分:0)

foreach($yourary as $key=>$value)
{
  if($value[REDUNDANT]=="YES")
  {
   unset($yourary[$key]);
  }
}