Php 为什么_array()返回false?
我有一个SimpleXML对象:Php 为什么_array()返回false?,php,arrays,oop,simplexml,Php,Arrays,Oop,Simplexml,我有一个SimpleXML对象: object(SimpleXMLElement)#176 (1) { ["record"]=> array(2) { [0]=> object(SimpleXMLElement)#39 (2) { ["f"]=> array(2) { [0]=> string(13) "stuff" [1]=> string(1) "1" } }
object(SimpleXMLElement)#176 (1) {
["record"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#39 (2) {
["f"]=>
array(2) {
[0]=>
string(13) "stuff"
[1]=>
string(1) "1"
}
}
[1]=>
object(SimpleXMLElement)#37 (2) {
["f"]=>
array(2) {
[0]=>
string(13) "more stuff"
[1]=>
string(3) "90"
}
}
}
为什么数组($object->record)返回false?它清楚地表明这是一个数组。为什么我不能使用is_数组检测它
此外,我无法使用(array)$object->record将其转换为数组。我得到这个错误:
警告:现在还不可能
为属性指定复杂类型
SimpleXML节点是可以包含其他SimpleXML节点的对象。使用迭代器到数组()。它不是数组。
var\u dump
输出具有误导性。考虑:
<?php
$string = <<<XML
<?xml version='1.0'?>
<foo>
<bar>a</bar>
<bar>b</bar>
</foo>
XML;
$xml = simplexml_load_string($string);
var_dump($xml);
var_dump($xml->bar);
?>
正如您在第二个
var\u dump
中所看到的,它实际上是一个simplexmlement
我使用count()
函数解决了这个问题:
if( count( $xml ) > 1 ) {
// $xml is an array...
}
永远不要相信SimpleXML的
var\u dump()
或print\r()
。我接受了这个答案,因为它允许我完成我的任务。谢谢大家的意见。
if( count( $xml ) > 1 ) {
// $xml is an array...
}