Reputation: 371
I currently have an variable that contains the below array. Each part of the array is user submitted how would I go about checking whether each part of the array is an integer value and if it's not then dropping that part of the array?
Array ( [0] => 3 [1] => 4 [2] => 17 [3] => 5 [4] => 6 [5] => 11 [6] => 7 [7] => 8 [8] => 9 [9] => 15 [10] => 16 [11] => 18 [12] => 10 [13] => 12 [14] => 13 [15] => 14 )
I'm storing the array inside $forums and I've tried array_map("ctype_digit", $forums);
Upvotes: 2
Views: 141
Reputation: 2755
Use array_filter
with is_int
$filtered = array_filter($array, 'is_int');
You can use like this. Simple.
Upvotes: 2
Reputation: 543
Loop through the array and check value array is numeric or not if not then delete that key.
foreach($yourArray as $key => $value)
{
if (!is_numeric($value))
{
unset($yourArray[$key]);
}
}
Upvotes: 0
Reputation: 4122
You can use array_filter
to get the result array with 'is_numeric'
parameter:
$yourArray = Array( [0] => 3 [1] => 4 [2] => 17 [3] => 5 [4] => 6 [5] => 11 [6] => 7 [7] => 8 [8] => 9 [9] => 15 [10] => 16 [11] => 18 [12] => 10 [13] => 12 [14] => 13 [15] => 14 );
$filtered = array_filter($yourArray, 'is_numeric');
Upvotes: 6
Reputation: 3731
Simply
<?php
$a = [1,2,3,4,5,6,'test',7,8,9];
foreach($a as $key => $value) {
if(!is_int($value)) {
unset($a[$key]);
}
}
Upvotes: 1