从多维数组中删除空值

时间:2015-01-15 15:39:20

标签: php arrays multidimensional-array

我研究了关于该主题的stackoverflow上发布的很多问题和答案,但我仍然无法找到解决以下问题的方法。

我需要根据此链接提供的文本生成一个数组,减去一些特定的行:https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf。这部分我能够做到。

PHP代码:

$arr = file('https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf');
$arr = preg_replace('/^(?![a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}\s).*$/i', '', $arr);
$arr = preg_replace ('/#/i', '',$arr);
$arr = preg_replace('/\t+/', ' ', $arr);
$arr = preg_replace('/\s{2,}/', ' ', $arr);
$arr = preg_replace('/ /', '~', $arr, 2);

foreach ($arr as $field) 
{
    $macList[] = explode('~', $field);
}

echo "<pre>";
print_r($macList);
echo "</pre>";

问题在于,当我使用print_r时,前52个子阵列是空的,加上底部有几十个。我无法弄清楚如何删除它们。

减少print_r输出,因为完整输出返回25770个子数组:

Array
(

[0] => Array
    (
        [0] => 

    )

[1] => Array
    (
        [0] => 

    )

[2] => Array
    (
        [0] => 

    )

[3] => Array
    (
        [0] => 

    )

[4] => Array
    (
        [0] => 

    )

[5] => Array
    (
        [0] => 

    )

[6] => Array
    (
        [0] => 

    )

[7] => Array
    (
        [0] => 

    )

[8] => Array
    (
        [0] => 

    )

[9] => Array
    (
        [0] => 

    )

[10] => Array
    (
        [0] => 

    )

[11] => Array
    (
        [0] => 

    )

[12] => Array
    (
        [0] => 

    )

[13] => Array
    (
        [0] => 

    )

[14] => Array
    (
        [0] => 

    )

[15] => Array
    (
        [0] => 

    )

[16] => Array
    (
        [0] => 

    )

[17] => Array
    (
        [0] => 

    )

[18] => Array
    (
        [0] => 

    )

[19] => Array
    (
        [0] => 

    )

[20] => Array
    (
        [0] => 

    )

[21] => Array
    (
        [0] => 

    )

[22] => Array
    (
        [0] => 

    )

[23] => Array
    (
        [0] => 

    )

[24] => Array
    (
        [0] => 

    )

[25] => Array
    (
        [0] => 

    )

[26] => Array
    (
        [0] => 

    )

[27] => Array
    (
        [0] => 

    )

[28] => Array
    (
        [0] => 

    )

[29] => Array
    (
        [0] => 

    )

[30] => Array
    (
        [0] => 

    )

[31] => Array
    (
        [0] => 

    )

[32] => Array
    (
        [0] => 

    )

[33] => Array
    (
        [0] => 

    )

[34] => Array
    (
        [0] => 

    )

[35] => Array
    (
        [0] => 

    )

[36] => Array
    (
        [0] => 

    )

[37] => Array
    (
        [0] => 

    )

[38] => Array
    (
        [0] => 

    )

[39] => Array
    (
        [0] => 

    )

[40] => Array
    (
        [0] => 

    )

[41] => Array
    (
        [0] => 

    )

[42] => Array
    (
        [0] => 

    )

[43] => Array
    (
        [0] => 

    )

[44] => Array
    (
        [0] => 

    )

[45] => Array
    (
        [0] => 

    )

[46] => Array
    (
        [0] => 

    )

[47] => Array
    (
        [0] => 

    )

[48] => Array
    (
        [0] => 

    )

[49] => Array
    (
        [0] => 

    )

[50] => Array
    (
        [0] => 

    )

[51] => Array
    (
        [0] => 

    )

[52] => Array
    (
        [0] => 00:00:00
        [1] => 00:00:00
        [2] => Officially Xerox, but 0:0:0:0:0:0 is more common

    )
)

问题:如何删除这52个(0到51)空数组?我没有尝试过任何工作。

2 个答案:

答案 0 :(得分:1)

而不是尝试删除空键,而不是在第一个位置添加空数组: 编辑似乎是空的&#39;数组实际上不是空的,而是包含一个带空格字符的单个元素。要删除,您可以将emptytrimarray_maparray_filter合并:

  foreach ($arr as $field)
  {
      $temp = explode('~', $field);
      if(!empty(array_filter(array_map('trim', $temp))))
      {
          $macList[] = $temp;
      }
  }

  echo "<pre>";
  print_r($macList);
  echo "</pre>";

答案 1 :(得分:1)

作为使用array_filter()假设的示例,它始终是一个空的单个元素数组:

$new_array = array_filter($array, function($elements) {
    return !empty($elements[0]);
});