5


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!

4

7 回答 7

7

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 hexadecimals.

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

于 2012-05-02T16:47:02.740 回答
5

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.";
}
于 2012-05-02T16:46:09.487 回答
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";
}
于 2012-05-02T16:50:41.587 回答
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.';
}
于 2012-05-02T17:40:24.307 回答
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;
}
于 2015-04-08T16:19:52.270 回答
0

Use gettype()

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

于 2012-05-02T16:47:22.117 回答
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.";
}
于 2012-05-02T16:49:35.627 回答