从多维数组中删除空值
Remove empty values from a multidimensional array
我研究了很多关于这个主题的问题和答案,但我仍然找不到解决以下问题的方法。
我需要根据此 link 提供的文本生成一个数组,减去一些特定行: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
输出,因为完整输出returns 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)个空数组?我试过都没有用。
与其尝试删除空键,不如不要在第一个位置添加空数组:
编辑似乎 'empty' 数组实际上不是空的,而是包含一个带有 space 字符的元素。要删除,您可以将 empty
与 trim
、 array_map
和 array_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>";
作为使用 array_filter()
和 的示例,假设 它始终是一个空的单元素数组:
$new_array = array_filter($array, function($elements) {
return !empty($elements[0]);
});