6


I am trying to detect if one or more variables contain numbers. I have tried a few different methods, but I have not been entirely successful.

Here is what I have tried.

<?php
$one = '1';
$two = '2';

$a1 = '3';
$a2 = '4';
$a3 = '5';


$string_detecting_array = array(); 

array_push($string_detecting_array, $one,$two,$a1,$a2,$a3); 

foreach ($string_detecting_array as $key) { 
    if (is_numeric($key)) {
        echo 'Yes all elements in array are type integer.';
    } 
    else {
        echo "Not all elements in array were type integer.";
    }
}

?>



I haven't been successful using this method. Any ideas? Thankyou in advance!

7 Answers 7

8

First off, your loop logic is wrong: you should process all the items in the array before reaching a verdict. The shortest (although not most obvious) way to do this is with

$allNumbers = $array == array_filter($array, 'is_numeric');

This works because array_filter preserves keys and comparing arrays with == checks element counts, keys, and values (and the values here are primitives, so can be trivially compared).

A more mundane solution would be

$allNumbers = true;
foreach ($array as $item) {
    if (!is_numeric_($item)) {
        $allNumbers = false;
        break;
    }
}

// now $allNumbers is either true or false

Regarding the filter function: if you only want to allow the characters 0 to 9, you want to use ctype_digit, with the caveat that this will not allow a minus sign in front.

is_numeric will allow signs, but it will also allow floating point numbers and scientific notation.

gettype will not work in this case because your array contains numeric strings, not numbers.

Sign up to request clarification or add additional context in comments.

Comments

6

You can use gettype if you want to explicitly know if the variable is a number. Using is_numeric will not respect types.

If you are intending to use is_numeric but want to know if all elements are, then proceed as follows:

$all_numeric = true;
foreach ($string_detecting_array as $key) { 
    if (!(is_numeric($key))) {
        $all_numeric = false;
        break;
    } 
}

if ($all_numeric) {
    echo 'Yes all elements in array are type integer.';
} 
else {
    echo "Not all elements in array were type integer.";
}

3 Comments

Hey, thanks for the quick response. I have tried this just now and it doesn't seem to work. I have just replaced is_numeric with gettype. Is that what you meant?
Try var_dump(gettype($one)) and see if that makes sense.
Hi, thanks for the example, it was very helpful! Works just how I wanted it to. My logic was clearly all wrong. Thankyou for clearing it up.
4

You can chain array_map with array_product to get a one-liner expression:

if (array_product(array_map('is_numeric', $string_detecting_array))) {
    echo "all values are numeric\n";
} else {
    echo "not all keys are numeric\n";
}

Comments

1

You can use this:

$set = array(1,2,'a','a1','1');  

if(in_array(false, array_map(function($v){return is_numeric($v);}, $set)))
{
    echo 'Not all elements in array were type integer.';
}
else
{
    echo 'Yes all elements in array are type integer.';
}

Comments

1

You can create own batch testing function. It may be static function on your utility class!

/**
 * @param array $array
 * @return bool
 */
public static function is_all_numeric(array $array){
    foreach($array as $item){
        if(!is_numeric($item)) return false;
    }
    return true;
}

Comments

0

Use gettype()

http://php.net/manual/en/function.gettype.php

Comments

0

You have to set a flag and look at all the items.

$isNumeric = true;
foreach ($string_detecting_array as $key) { 
    if (!is_numeric($key)) {
        $isNumeric = false;
    }
}

if ($isNumeric) {
    echo 'Yes all elements in array are type integer.';
} 
else {
    echo "Not all elements in array were type integer.";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.