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

  • Home
  • SEARCH
  • 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 1037255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:49:52+00:00 2026-05-16T14:49:52+00:00

How do you verify an array contains only values that are integers? I’d like

  • 0

How do you verify an array contains only values that are integers?

I’d like to be able to check an array and end up with a boolean value of true if the array contains only integers and false if there are any other characters in the array. I know I can loop through the array and check each element individually and return true or false depending on the presence of non-numeric data:

For example:

$only_integers = array(1,2,3,4,5,6,7,8,9,10);
$letters_and_numbers = array('a',1,'b',2,'c',3);

function arrayHasOnlyInts($array)
{
    foreach ($array as $value)
    {
        if (!is_int($value)) // there are several ways to do this
        {
             return false;
        }
    }
    return true;
}

$has_only_ints = arrayHasOnlyInts($only_integers ); // true
$has_only_ints = arrayHasOnlyInts($letters_and_numbers ); // false

But is there a more concise way to do this using native PHP functionality that I haven’t thought of?

Note: For my current task I will only need to verify one dimensional arrays. But if there is a solution that works recursively I’d be appreciative to see that to.

  • 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-05-16T14:49:53+00:00Added an answer on May 16, 2026 at 2:49 pm
    $only_integers       === array_filter($only_integers,       'is_int'); // true
    $letters_and_numbers === array_filter($letters_and_numbers, 'is_int'); // false
    

    It helps to define two helper, higher-order functions:

    /**
     * Tell whether all members of $elems validate the $predicate.
     *
     * all(array(), 'is_int')           -> true
     * all(array(1, 2, 3),   'is_int'); -> true
     * all(array(1, 2, 'a'), 'is_int'); -> false
     */
    function all($elems, $predicate) {
      foreach ($elems as $elem) {
        if (!call_user_func($predicate, $elem)) {
          return false;
        }
      }
    
      return true;
    }
    
    /**
     * Tell whether any member of $elems validates the $predicate.
     *
     * any(array(), 'is_int')               -> false
     * any(array('a', 'b', 'c'), 'is_int'); -> false
     * any(array(1, 'a', 'b'),   'is_int'); -> true
     */
    function any($elems, $predicate) {
      foreach ($elems as $elem) {
        if (call_user_func($predicate, $elem)) {
          return true;
        }
      }
    
      return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form and would like to verify that a few fields that
I'm working with an array that looks like this: cards = [#<Cardshark::Card:0x9200cc @rank=:ace, @suit=:coins>,
I like RSpec, and I really like its =~ array operator matcher to verify
Ive created the a button $form['submit'] = array( '#type' => 'submit', '#value' => 'verify',
I need some help getting the if statement to verify that the string Array
Is there a verify() function (Such as VERIFY() in msvc) or similar that is
I need to verify a procedure that has an out cursor parameter. Specifically I
I need to verify that a code generated in one class is same as
How can I verify that passing block is execute correctly ? - (void)locationManager:(CLLocationManager *)manager
While trying to verify to myself, that C# Equals for IEnumerables is a reference

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.