【问题标题】:Difficulty checking if elements in array are type integer PHP难以检查数组中的元素是否为整数 PHP
【发布时间】:2012-05-12 04:25:54
【问题描述】:


我正在尝试检测一个或多个变量是否包含数字。我尝试了几种不同的方法,但都没有完全成功。

这是我尝试过的。

<?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.";
    }
}

?>



我没有成功使用这种方法。有任何想法吗?提前谢谢你!

【问题讨论】:

    标签: php arrays validation isnumeric


    【解决方案1】:

    首先,您的循环逻辑是错误的:您应该在得出结论之前处理 所有 数组中的项目。最短(虽然不是最明显)的方法是使用

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

    之所以有效,是因为array_filter 保留键,而comparing arrays with == 检查元素计数、键、值(这里的值是原始值,因此可以简单地比较)。

    一个更普通的解决方案是

    $allNumbers = true;
    foreach ($array as $item) {
        if (!is_numeric_($item)) {
            $allNumbers = false;
            break;
        }
    }
    
    // now $allNumbers is either true or false
    

    关于过滤功能:如果您只想允许字符09,您想使用ctype_digit,但需要注意的是这不允许前面有减号。

    is_numeric 允许使用符号,但也允许使用浮点数和十六进制。

    gettype 在这种情况下不起作用,因为您的数组包含数字字符串,而不是数字。

    【讨论】:

      【解决方案2】:

      如果您想明确知道变量是否为数字,可以使用gettype。使用 is_numeric 不会尊重类型。

      如果您打算使用is_numeric,但想知道所有元素是否都在,请按以下步骤操作:

      $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.";
      }
      

      【讨论】:

      • 您好,感谢您的快速回复。我刚刚尝试过这个,它似乎不起作用。我刚刚用 gettype 替换了 is_numeric。你是这个意思吗?
      • 试试var_dump(gettype($one)) 看看是否有意义。
      • 嗨,谢谢你的例子,它很有帮助!就像我想要的那样工作。我的逻辑显然全错了。谢谢你清理它。
      【解决方案3】:

      您可以将array_maparray_product 链接起来以获得单行表达式:

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

      【讨论】:

        【解决方案4】:

        你可以用这个:

        $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.';
        }
        

        【讨论】:

          【解决方案5】:

          您可以创建自己的批量测试功能。它可能是您的实用程序类上的静态函数!

          /**
           * @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;
          }
          

          【讨论】:

            【解决方案6】:

            【讨论】:

              【解决方案7】:

              你必须设置一个标志并查看所有项目。

              $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.";
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-12-07
                • 1970-01-01
                • 2018-05-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多