Possible Duplicate:
More concise way to check to see if an array contains only numbers (integers)
Is there a way to check that all values in an single dimension array are ints?
The best I could do is something like
function checkArray($array) {
foreach($array as $value) {
if (!is_int($value)) return false;
}
return true;
}
I’m wondering if there’s a more concise/pre-built way, something like is_int_array() that I might not know about.
I imagine you can never beat
O(n)as all the elements need to be checked that they conform to the rule. The below checks each element onceO(n)and removes it, if it is not an integer then does a simple comparison.Still will have a slightly larger storage complexity however (needs to store the filtered array).
O(n)is a representation of complexity, in this case the complexity isn(the number of elements in the array) as each element must be looked at once.If for example you wanted to multiple every number by every other number the complexity is approximately
O(n^2)as for each element you must look at each other element (though this is a poor example)See this guide for further information on Big O Notation as it is called
However try the below (adapted from previous question)
You could then do