-2

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.

Community
  • 1
  • 1
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • A better way than looping and checking? – Paul Dessert Sep 10 '12 at 18:27
  • 2
    Whenever you said "I want to do some operation that involves all elements of an array", than, be sure, the best you can do is `O(n)`. If you do better than that, it means you have some other piece of data in which you stored information, and the actual information is less than `O(n)`(but you still need `O(n)` to get that information). This means: no, you can't do better. The only way would be to check when you insert them, but you are actually doing the same number of operations, just looping once when populating it. So any solution has actually same complexity, maybe a bit less overhead. – Bakuriu Sep 10 '12 at 18:31
  • 1
    I'd almost bet precious parts of my anatomy that the performance bottleneck of your application is **not** in looping through arrays to see if they consist of integer elements. –  Sep 10 '12 at 18:39

1 Answers1

2

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 once O(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 is n (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)

if($only_integers === array_filter($only_integers, 'is_int')); // true
if($letters === array_filter($letters, 'is_int')); // false

You could then do

/**
 * Test array against provided filter
 * testFilter(array(1, 2, 'a'), 'is_int'); -> false
 */
function testFilter($array, $test) {
    return array_filter($array, $test) === $array;
}
Community
  • 1
  • 1
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
  • 1
    -1 Not an answer if you're referring another answer. – Kermit Sep 10 '12 at 18:38
  • @SteveRobbins `O(n)` is a notation used to represent processing time – Kermit Sep 10 '12 at 18:39
  • @SteveRobbins - It's an instance of [big O Notation](http://en.wikipedia.org/wiki/Big_O_notation). In other words, it's part of what it means to say that one algorithm is faster than another. Why would you ask a question about algorithm speed without knowing about big O notation?! –  Sep 10 '12 at 18:40
  • @njk I disagree, I've added further information on top of the other answer specific to this question – Pez Cuckow Sep 10 '12 at 18:41
  • 2
    @PezCuckow Sorry, I was still seeing your original answer that just referred to the other answer. – Kermit Sep 10 '12 at 18:42