我有一个如下所示的数组,它是通过解析xml url生成的.
阵列是
Array
(
[Tags] => SimpleXMLElement Object
(
[0] =>
)
)
Run Code Online (Sandbox Code Playgroud)
数组名称是$result
.现在我想检查一下,如果像上面那样收到数组,我想打印一条失败的消息.但是如果在条件下如何检查这个数组呢?
Mar*_*son 38
您可以使用
empty($result)
Run Code Online (Sandbox Code Playgroud)
检查主阵列是否为空.
但是,由于您有一个SimpleXMLElement对象,因此如果该对象为空,则需要查询该对象.见http://www.php.net/manual/en/simplexmlelement.count.php
例如:
if (empty($result) || !isset($result['Tags'])) {
return false;
}
if ( !($result['Tags'] instanceof SimpleXMLElement)) {
return false;
}
return ($result['Tags']->count());
Run Code Online (Sandbox Code Playgroud)
这将检查数组是否为空
if (!empty($result) {
// do stuf if array is not empty
} else {
// do stuf if array is empty
}
Run Code Online (Sandbox Code Playgroud)
这将检查数组是否为空
if (is_null($result) {
// do stuf if array is null
} else {
// do stuf if array is not null
}
Run Code Online (Sandbox Code Playgroud)