php 如何检查变量是否是数组?...或类似数组的东西
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15603952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if variable is array?... or something array-like
提问by Voitcus
I want to use a foreachloop with a variable, but this variable can be many different types, NULLfor example.
我想使用一个foreach带有变量的循环,但NULL例如,这个变量可以是许多不同的类型。
So before foreachI test it:
所以在foreach我测试之前:
if(is_array($var)){
foreach($var as ...
But I realized that it can also be a class that implements Iteratorinterface. Maybe I am blind but how to check whether the class implements interface? Is there something like is_afunction or inheritsoperator? I found class_implements, I can use it, but maybe there is something simpler?
但是我意识到它也可以是一个实现Iterator接口的类。也许我瞎了,但是如何检查类是否实现了接口?有is_a函数或inherits运算符之类的东西吗?我发现class_implements,我可以使用它,但也许有更简单的东西?
And second, more important, I suppose this function exist, would be enough to check if the variable is_arrayor "implements Iteratorinterface" or should I test for something more?
其次,更重要的是,我认为这个函数存在,足以检查变量is_array或“实现Iterator接口”是否应该测试更多内容?
采纳答案by Shoe
If you are using foreachinside a function and you are expecting an array or a Traversableobject you can type hint that function with:
如果您foreach在函数内部使用并且您需要一个数组或一个Traversable对象,您可以输入提示该函数:
function myFunction(array $a)
function myFunction(Traversable)
If you are not using foreachinside a function or you are expecting both you can simply use this constructto check if you can iterate over the variable:
如果您不在foreach函数内部使用,或者您期望两者都使用,则可以简单地使用此构造来检查是否可以迭代变量:
if (is_array($a) or ($a instanceof Traversable))
回答by hakre
foreachcan handle arrays and objects. You can check this with:
foreach可以处理数组和对象。您可以通过以下方式检查:
$can_foreach = is_array($var) || is_object($var);
if ($can_foreach) {
foreach ($var as ...
}
You don't need to specifically check for Traversableas others have hinted it in their answers, because all objects - like all arrays - are traversable in PHP.
您不需要Traversable像其他人在他们的答案中暗示的那样专门检查,因为所有对象 - 就像所有数组一样 - 在 PHP 中都是可遍历的。
More technically:
从技术上讲:
foreachworks with all kinds of traversables, i.e. with arrays, with plain objects (where the accessible properties are traversed) andTraversableobjects (or rather objects that define the internalget_iteratorhandler).
foreach适用于各种可遍历对象,即数组、普通对象(遍历可访问属性的地方)和Traversable对象(或者更确切地说是定义内部get_iterator处理程序的对象)。
(source)
(来源)
Simply said in common PHP programming, whenever a variable is
简单地说,在普通的 PHP 编程中,每当一个变量
- an array
- an object
- 数组
- 一个东西
and is not
并且不是
- NULL
- a resource
- a scalar
- 空值
- 一种资源
- 标量
you can use foreachon it.
你可以使用foreach它。
回答by Baba
You can check instance of Traversablewith a simple function. This would work for all this of Iteratorbecause Iterator extends Traversable
您可以Traversable使用一个简单的函数检查实例。这对所有这些都有效,Iterator因为Iterator extends Traversable
function canLoop($mixed) {
return is_array($mixed) || $mixed instanceof Traversable ? true : false;
}
回答by Alexey
<?php
$var = new ArrayIterator();
var_dump(is_array($var), ($var instanceof ArrayIterator));
returns bool(false)or bool(true)
返回bool(false)或bool(true)
回答by Eduardo Cuomo
Functions
职能
<?php
/**
* Is Array?
* @param mixed $x
* @return bool
*/
function isArray($x) : bool {
return !isAssociative($x);
}
/**
* Is Associative Array?
* @param mixed $x
* @return bool
*/
function isAssociative($x) : bool {
if (!is_array($array)) {
return false;
}
$i = count($array);
while ($i > 0) {
if (!isset($array[--$i])) {
return true;
}
}
return false;
}
Example
例子
<?php
$arr = [ 'foo', 'bar' ];
$obj = [ 'foo' => 'bar' ];
var_dump(isAssociative($arr));
# bool(false)
var_dump(isAssociative($obj));
# bool(true)
var_dump(isArray($obj));
# bool(false)
var_dump(isArray($arr));
# bool(true)
回答by faintsignal
Since PHP 7.1 there is a pseudo-type iterablefor exactly this purpose. Type-hinting iterableaccepts any array as well as any implementation of the Traversableinterface. PHP 7.1 also introduced the function is_iterable(). For older versions, see other answers here for accomplishing the equivalent type enforcement without the newer built-in features.
从 PHP 7.1 开始,有一个伪类型就是iterable为了这个目的。类型提示iterable接受任何数组以及Traversable接口的任何实现。PHP 7.1 也引入了该功能is_iterable()。对于旧版本,请参阅此处的其他答案,以在没有较新的内置功能的情况下完成等效的类型强制。
Fair play: As BlackHole pointed out, this question appears to be a duplicate of Iterable objects and array type hinting?and his or her answergoes into further detail than mine.
公平竞赛:正如 BlackHole 所指出的,这个问题似乎是Iterable 对象和数组类型提示的重复?和他或她的答案进入更详细的比我的。

