删除空数组元素 [英] Remove empty array elements

查看:143
本文介绍了删除空数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数组的某些元素是一个空字符串。我需要删除这些元素。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆