Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing non-integer entries from an array

Tags:

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?

like image 913
André Avatar asked Dec 26 '11 21:12

André


People also ask

How do you remove unwanted elements from an array?

pop() function: This method is used to remove elements from the end of an array. shift() function: This method is used to remove elements from the start of an array. splice() function: This method is used to remove elements from the specific index of an array.


1 Answers

Use array_filter with is_int

$filtered = array_filter($array, 'is_int');

Edit:

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');
like image 182
adlawson Avatar answered Sep 29 '22 15:09

adlawson