【问题标题】:Determine the type of array element in php在php中判断数组元素的类型
【发布时间】:2012-03-21 09:34:49
【问题描述】:

我正在调用一个返回数组的函数。 for 循环迭代给出以下输出。

string(22) "text/xml;charset=UTF-8"

字符串(7)“分块”

字符串(4)“gzip”

array(2) { ["Expect"]=> string(12) "100-continue" ["Content-Type"]=> string(48) "application/x-www-form-urlencoded; charset=utf-8" }

object(CFSimpleXML)#10 (1) { [0]=> string(6) "123456" }

如何检查数组元素是对象还是字符串?

【问题讨论】:

    标签: php


    【解决方案1】:
    foreach ($array as $element) {
      if (is_array($element)) {
        // array
      } else if (is_string($element)) {
        // string
      } else if (is_int($element)) {
        // int
      } else if (is_float($element)) {
        // float
      } else if (is_bool($element)) {
        // bool
      } else if (is_object($element)) {
        // object
      } else if (is_resource($element)) {
        // resource
      } else {
        // null/invalid type (you could add an === NULL if you want, I suppose)
      }
    }
    

    还有get_type()typeof 运算符,但由于这些返回的字符串在未来的PHP 版本中可能会发生变化,因此is_*() 函数更可靠。

    【讨论】:

      【解决方案2】:

      我更喜欢 switch-trick 解决方案:

      foreach ($array as $element) {
          switch(true)
          {
              case is_array($element):
                  // array
                  break;
              case is_string($element):
                  // string
                  break;
              case is_int($element):
                  // int
                  break;
              case is_float($element):
                  // float
                  break;
              case is_bool($element):
                  // bool
                  break;
              case is_object($element):
                  // object
                  break;
              case is_resource($element):
                  // resource
                  break;
              default:
                  // null
          }
      }
      

      【讨论】:

        【解决方案3】:

        你可以通过这种方式获取数组元素类型列表

        $types = array_map('gettype', $array);
        

        【讨论】:

          【解决方案4】:
          if (is_object($arrayElement)) ...
          if (is_array($arrayElement))  ...
          if (is_string($arrayElement)) ...
          

          【讨论】:

            【解决方案5】:
            【解决方案6】:

            您可以使用 PHP 的 gettype 函数,然后以简单的方式进行所需的操作 切换案例代码。

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

            请看这里:http://codepad.org/LRJcrKjJ

            <?php
            
                    $data = array(1, 1.,'hello', NULL, new stdClass);
            
                    foreach($data as $item)
                    {
                      $currType = gettype($item);
                      switch($currType){
                      case "integer" :
                        echo "I am integer ".$item." , double of me =  ".($item*2)."\n";
                        break;
                      case "string" :
                        echo "I am string  ".$item." , reverse of me = ".strrev($item)."\n";
                        break;
                      default:
                        echo "I am ".$currType ."\n" ;
                        break;
                      }
            
                    }
            
                ?>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-09-27
              • 2011-11-02
              • 2021-08-20
              • 2017-02-08
              • 1970-01-01
              • 1970-01-01
              • 2020-12-22
              • 1970-01-01
              相关资源
              最近更新 更多