【问题标题】:How to differentiate a 1-dim array from multi-dim array that have the same count?如何区分具有相同计数的一维数组和多维数组?
【发布时间】:2017-06-12 17:53:57
【问题描述】:

我有 2 个潜在的响应数组,我不确定我会得到哪一个。 但我知道它将是以下之一:

  • 1 - 可能包含具有键和值的数组
  • 2 - 可能只包含键和值

我的目标是检查我的回答是否属于该类别之一,并以此为基础做我的逻辑。

我尝试使用 PHP count() 函数,但它们都返回 2 - 这是相同的值

我应该检查什么才能知道我收到的是什么类型的响应?

数组#1

array:2 [▼

  0 => array:2 [▼

        "content" => "Administrator"
        "XSI:TYPE" => "xs:string"

    ]
      1 => array:2 [▼

        "content" => "Read Only"
        "XSI:TYPE" => "xs:string"

    ]
]

数组#2

array:2 [▼

  "content" => "Read Only"
  "XSI:TYPE" => "xs:string"
]

【问题讨论】:

  • 当然,数量是一样的。第一个包含两个数组,第二个包含两个字符串。
  • 你试过array_diff()吗?
  • @Don'tPanic:用什么 PHP 函数来检查两者之间的区别?你知道吗?
  • 既然您知道数组键,这将是检查if(isset($myArray['content'])){ } else { }的最快方法
  • @cmorrissey:啊!!!绝妙的想法......

标签: php arrays multidimensional-array data-structures


【解决方案1】:

我同意 proposed by cmorrissey in the comments 最好的方式:

由于您知道数组键,这将是检查的最快方法
if(isset($myArray['content'])){ } else { }


对于你不知道数组键是什么的情况,我之前的回答应该仍然有效:

从数组中获取第一项。

$first = reset($array);

那就数数吧。

if (count($first) > 1) {
    // it's like array 1
} else {
    // it's like array 2  ( count($any_string) always returns 1 )
}

您也可以使用is_array($first)。这可能会快一点。

【讨论】:

    【解决方案2】:

    我认为这将帮助您区分两个相同计数的数组。您可以将COUNT_RECURSIVE 标志与count 函数一起使用。

    Try this code snippet here

    if(count($array1,COUNT_RECURSIVE)==count($array2,COUNT_RECURSIVE))
    {
        echo "Both's value are strings with equal count";
    }
    elseif(count($array1,COUNT_RECURSIVE)>count($array2,COUNT_NORMAL))
    {
        echo "Array one is array of arrays";
    }
    else
    {
        echo "Array two is array of arrays";
    }
    

    【讨论】:

      【解决方案3】:

      我知道您需要知道一个数组中是否存在另一个数组。我有这个可行的解决方案。

      我们可以有这样的数组:

      $array = array(1, "string", array(1, 2));
      

      它有一个整数、字符串和一个数组。我们可以使用 gettype() 函数来获取事物的类型。

      在这个例子中:

       foreach($array as $row) {
         echo gettype($row) . '<br>';
       }
      

      我们有下一个结果:

      integer
      string
      array
      

      我们可以这样做:

      foreach($array as $row) {
        if(gettype($row) == 'array') {
          echo "This is an array<br>";
        } else {
          echo "Keep trying guy...<br>";
        }
      }
      

      结果会是:

      Keep trying guy...
      Keep trying guy...
      This is an array
      

      希望对您有所帮助,并记住“热爱编码”:)

      【讨论】:

        【解决方案4】:

        不完全确定我是否逐字理解问题,但我最初的感觉是您的意思是检查父数组是否有任何子数组元素?

        那么,我想你的意思是?

        foreach ($response as $element) {
        
            if (is_array($element)) {
                // The element is an array
            } else {
                // The element is not an array
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2018-09-20
          • 2019-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多