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?
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.
Use array_filter
with 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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With