删除空数组元素 [英] Remove empty array elements
问题描述
在我的数组的某些元素是一个空字符串。我需要删除这些元素。
Some elements in my array are an empty string. I need to remove those elements.
code:
foreach($linksArray as $link)
{
if($link == '')
{
unset($link);
}
}
print_r($linksArray);
但它不工作, $ linksArray
还有空元素。我也试图与做空()
功能,但结果是一样的。
But it doesn't work, $linksArray
still has empty elements. I have also tried doing it with the empty()
function but the outcome is the same.
推荐答案
只需使用 array_filter()
,该处理方便这一切都为你:
Simply use array_filter()
, which conveniently handles all this for you:
print_r(array_filter($linksArray));
您在你的如果
条件的错字:它应该是 $链接
,而不是 $链接
。此外,为了修改一个数组的元素在foreach循环中,你需要引用变量,即为&放大器; $链接
而不是为$链接
。
You have a typo in your if
condition: it should be $link
, not $links
. Furthermore, in order to modify the elements of an array in a foreach loop, you need to reference the variable, i.e. as &$link
instead of as $link
.
这篇关于删除空数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!