Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:46:26+00:00 2026-06-11T15:46:26+00:00

Possible Duplicate: More concise way to check to see if an array contains only

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:46:27+00:00Added an answer on June 11, 2026 at 3:46 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: More concise way to check to see if an array contains only
Possible Duplicate: Is there a more efficent way to randomise a set of LINQ
Possible Duplicate: How can JavaScript make new page that contains more JavaScript? I tried
Possible Duplicate: JavaScript equivalent to printf/string.format Is there a more C like way to
Possible Duplicate: More efficient way to remove elements from an array list I have
Possible Duplicate: How to check if resultset has one row or more? What will
Possible Duplicate: Python program to find fibonacci series. More Pythonic way. Hey, i was
Possible Duplicate: How can I find the missing value more concisely? Is there a
Possible Duplicate: Why isn't more Java software compiled natively? I know that Java is
Possible Duplicate: More concise version of max/min without the block If I had N

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.