开发者

how to get rid of empty elements

I have an array thats generated dynamically and it has some empty elements. How do I get rid of empty elements from an array?

array

0 => string '' (length=0)

1 => string 'x%6wm' (length=5)

2 => string 'x%6wmvf' (length=7) 3 => string 'x%645' (length=5) 4 => string '' (length=0)

And I want it to become like

array

0 => string 'x%6wm' (length=5)

1 => string 'x%6wmvf' (le开发者_开发知识库ngth=7)

2 => string 'x%645' (length=5)

Thanks


You can combine the functions array_filter() and array_values() to accomplish your goal.

$cleanArray = array_values(array_filter($array));


You can loop throuh it and check each element of the array if empty or not and if it's empty you can delete that element.


Per above the array filter and values would work very well for this.

$array = array("", "Hello", "", "World", "");

$info = array_values(array_filter($array));

print_r($info);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜