从数组中删除非整数条目 [英] Removing non-integer entries from an array
本文介绍了从数组中删除非整数条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的PHP数组:
I have a PHP array like this one:
array( [0] => 1
[1] => 2
[2] => 3
[3] => Some strings
)
如何从数组中删除不是整数的条目?我需要输出:
How can I remove an entry that is not an integer number from an array? I need to output this:
array( [0] => 1
[1] => 2
[2] => 3
)
有人可以给我一个线索吗?
Can someone give me a clue?
推荐答案
使用 array_filter
和 is_int
$filtered = array_filter($array, 'is_int');
如评论中所述,使用以下一种方法可能是更好的解决方案.
As noted in the comments, it may be a better solution to use one of the following instead.
$filtered = array_filter($array, 'is_numeric');
$filtered = array_filter($array, 'ctype_digit');
这篇关于从数组中删除非整数条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文