在PHP中有选择地从关联数组中删除空元素

时间:2016-10-19 08:14:41

标签: php arrays associative-array array-map array-filter

我有以下关联数组:

Array
(
    [0] => Array
        (
            [0] => 18-Jul-16
            [1] => 29-Jul-15
            [2] => 2-Feb-16
            [3] => 3301
            [4] => 1800 Bimodel
            [5] => 5813
            [6] => 1 800 Bimodel Multi-Option Test
            [7] => Tested
            [8] => Shop Center
            [9] => Shop Services
            [10] => 864
            [11] => 20
            [12] => 884
            [13] => 0.75
            [14] => 0.2
            [15] => 0.49
            [16] => 429.6
            [17] => 47.3
            [18] => 382.3
            [19] => 
            [20] => Jonas John
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 
        )
)

要过滤掉空数组元素,我使用以下代码:

            $arrData = array_map('array_filter', $arrData);
            $arrData = array_filter($arrData);

上面的代码从数组中删除所有空元素。它还会删除第0个数组中索引19处的值。我希望它只在数组的所有值都为空时才删除数组元素。在我的情况下,只应删除索引1处的数组,并且不应删除第0个索引处的数组(或其任何元素)。我将始终需要每个数组中的21个元素。我可以选择性地使用PHP代码吗?

由于

4 个答案:

答案 0 :(得分:1)

 foreach($dataArray as $outerKey =>$innerArray) {
 $counter=0;         
 foreach($innerArray as $key=>$value){
      if(trim($value)==""){
        $counter++;
      } 
          if($counter==count($innerArray)
              unset($dataArray[$outerKey]);
    }
 }

答案 1 :(得分:0)

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return $value !== ''; }));

答案 2 :(得分:0)

简直就是没有魔力:

<?php
/**
$data contains (incoming) unfiltered value
*/
$data = [
    'keep1' => [
        0,1,2,3
    ]
    ,
    'drop1'=>[
        '',0,false,[]
    ]
    ,'keep2'=>[
        2,2,3,4
    ]
];

foreach ($data as $key => $innerArray) { //check for each element
    foreach ($innerArray as $innerValue) {
        if ( !empty($innerValue) ) {
            continue 2;//stop investigating at first non empty, we shoud keep this
        }
    }
    //all values in innerArray are empty, drop this
    unset($data[$key]);

}

print_r(array_keys($data));

输出:

Array
(
    [0] => keep1
    [1] => keep2
)

答案 3 :(得分:0)

我认为有一个非常简单的解决方案。我最近有同样的问题。只需使用

$$data = array_filter($$data, 'array_filter');

array_filter()bool convert to false的数组中删除元素。例如,其中包括[], null, '', 0, false中的每一个。

功能

  1. 第一个array_filter()接受每个子数组,然后将其放入第二个array_filter()
  2. 第二个array_filter()删除每个false元素并返回其余元素。重要的是要注意,如果每个元素都转换为false,则返回的数组将为空。
  3. 第一个array_filter()从第二个结果中提取并检查false。因此,万一返回一个空数组,子数组将从数组中删除。

为了证明这种情况,我使用了上面cske的数据:

<?php

$data = [
    'keep1' => [0,1,2,3
    ],
    'drop1' => ['',0,false,[]
    ],
    'keep2' => [2,2,3,4
    ],
];
print_r(array_filter($data, 'array_filter'));

输出

Array
(
[keep1] => Array
    (
        [0] => 0
        [1] => 1
        [2] => 2
        [3] => 3
    )

[keep2] => Array
    (
        [0] => 2
        [1] => 2
        [2] => 3
        [3] => 4
    )

)