Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More concise way to check to see if an array contains only numbers (integers)

Tags:

arrays

php

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.

like image 387
John Conde Avatar asked Aug 24 '10 18:08

John Conde


People also ask

How do you check if an array is only integers?

To check if an array contains only numbers:Call the every() method, passing it a function. On each iteration, check if the type of of the current element is number . The every method returns true , only if the condition is met for every array element.

How do you check if an array contains a certain number?

The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

How do you check if there are values in an array?

Using Numpy array, we can easily find whether specific values are present or not. For this purpose, we use the “in” operator. “in” operator is used to check whether certain element and values are present in a given sequence and hence return Boolean values 'True” and “False“.

How do I check if an array contains only numbers in PHP?

php $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){ $test = implode('',$array); return is_numeric($test); } echo "numbers:".


1 Answers

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

It would help you in the future to define two helper, higher-order functions:

/**  * Tell whether all members of $array validate the $predicate.  *  * all(array(1, 2, 3),   'is_int'); -> true  * all(array(1, 2, 'a'), 'is_int'); -> false  */ function all($array, $predicate) {     return array_filter($array, $predicate) === $array; }  /**  * Tell whether any member of $array validates the $predicate.  *  * any(array(1, 'a', 'b'),   'is_int'); -> true  * any(array('a', 'b', 'c'), 'is_int'); -> false  */ function any($array, $predicate) {     return array_filter($array, $predicate) !== array(); } 
like image 176
Ionuț G. Stan Avatar answered Oct 18 '22 07:10

Ionuț G. Stan