This question already has an answer here:
这个问题在这里已有答案:
- How to remove empty values from multidimensional array in PHP? 8 answers
- 如何从PHP中的多维数组中删除空值? 8个答案
I have this array:
我有这个数组:
$aryMain = array(array('hello','bye'), array('',''),array('',''));
It is formed by reading a csv file and the array('','') are the empty rows at the end of the file.
它是通过读取csv文件而形成的,数组('','')是文件末尾的空行。
How can I remove them?
我该如何删除它们?
I've tried:
我试过了:
$aryMain = array_filter($aryMain);
But it is not working :(
但它不起作用:(
Thanks a lot!
非常感谢!
3 个解决方案
#1
17
To add to Rikesh's answer:
要添加到Rikesh的答案:
<?php
$aryMain = array(array('hello','bye'), array('',''),array('',''));
$aryMain = array_filter(array_map('array_filter', $aryMain));
print_r($aryMain);
?>
Sticking his code into another array_filter will get rid of the entire arrays themselves.
将他的代码粘贴到另一个array_filter中将自己摆脱整个数组。
Array
(
[0] => Array
(
[0] => hello
[1] => bye
)
)
Compared to:
相比:
$aryMain = array_map('array_filter', $aryMain);
Array
(
[0] => Array
(
[0] => hello
[1] => bye
)
[1] => Array
(
)
[2] => Array
(
)
)
#2
9
Use array_map along with array_filter,
将array_map与array_filter一起使用,
$array = array_filter(array_map('array_filter', $array));
Or just create a array_filter_recursive
function
或者只是创建一个array_filter_recursive函数
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input);
}
DEMO。
Note: that this will remove items comprising '0' (i.e. string with a numeral zero). Just pass 'strlen'
as a second parameter to keep 0
注意:这将删除包含'0'的项目(即带有数字零的字符串)。只需将'strlen'作为第二个参数传递即可保持0
#3
2
Apply array_filter()
on the main array and then once more on the inner elements:
在主数组上应用array_filter(),然后在内部元素上再次应用:
$aryMain = array_filter($aryMain, function($item) {
return array_filter($item, 'strlen');
});
The inner array_filter()
specifically uses strlen()
to determine whether the element is empty; otherwise it would remove '0'
as well.
内部array_filter()专门使用strlen()来确定元素是否为空;否则它也会删除'0'。
To determine the emptiness of an array you could also use array_reduce()
:
要确定数组的空虚,您还可以使用array_reduce():
array_filter($aryMain, function($item) {
return array_reduce($item, function(&$res, $item) {
return $res + strlen($item);
}, 0);
});
Whether that's more efficient is arguable, but it should save some memory :)
是否更高效是有争议的,但它应该节省一些内存:)
#1
17
To add to Rikesh's answer:
要添加到Rikesh的答案:
<?php
$aryMain = array(array('hello','bye'), array('',''),array('',''));
$aryMain = array_filter(array_map('array_filter', $aryMain));
print_r($aryMain);
?>
Sticking his code into another array_filter will get rid of the entire arrays themselves.
将他的代码粘贴到另一个array_filter中将自己摆脱整个数组。
Array
(
[0] => Array
(
[0] => hello
[1] => bye
)
)
Compared to:
相比:
$aryMain = array_map('array_filter', $aryMain);
Array
(
[0] => Array
(
[0] => hello
[1] => bye
)
[1] => Array
(
)
[2] => Array
(
)
)
#2
9
Use array_map along with array_filter,
将array_map与array_filter一起使用,
$array = array_filter(array_map('array_filter', $array));
Or just create a array_filter_recursive
function
或者只是创建一个array_filter_recursive函数
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input);
}
DEMO。
Note: that this will remove items comprising '0' (i.e. string with a numeral zero). Just pass 'strlen'
as a second parameter to keep 0
注意:这将删除包含'0'的项目(即带有数字零的字符串)。只需将'strlen'作为第二个参数传递即可保持0
#3
2
Apply array_filter()
on the main array and then once more on the inner elements:
在主数组上应用array_filter(),然后在内部元素上再次应用:
$aryMain = array_filter($aryMain, function($item) {
return array_filter($item, 'strlen');
});
The inner array_filter()
specifically uses strlen()
to determine whether the element is empty; otherwise it would remove '0'
as well.
内部array_filter()专门使用strlen()来确定元素是否为空;否则它也会删除'0'。
To determine the emptiness of an array you could also use array_reduce()
:
要确定数组的空虚,您还可以使用array_reduce():
array_filter($aryMain, function($item) {
return array_reduce($item, function(&$res, $item) {
return $res + strlen($item);
}, 0);
});
Whether that's more efficient is arguable, but it should save some memory :)
是否更高效是有争议的,但它应该节省一些内存:)