我有一个如下所示的数组,它是通过解析 xml url 生成的。
数组是
Array
(
[Tags] => SimpleXMLElement Object
(
[0] =>
)
)
数组名称是$result
。现在我想检查一下数组是否像上面那样接收到,我想打印一条失败消息。但是如何在 if 条件下检查这个数组呢?
最佳答案
你可以使用
empty($result)
检查主数组是否为空。
但由于您有一个 SimpleXMLElement 对象,您需要查询该对象是否为空。见 http://www.php.net/manual/en/simplexmlelement.count.php
例如:
if (empty($result)) {
return false;
}
if ( !($result['Tags'] instanceof SimpleXMLElement)) {
return false;
}
return ($result['Tags']->count());
关于php - 在php中检查数组是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8068220/