2010-11-30 160 views
6

我有这个SimpleXML的对象:为什么is_array()返回false?

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" 
    } 
    } 
} 

为什么is_array($对象 - >记录)返回false?它清楚地表明它是一个数组。为什么我无法使用is_array检测它?

另外,我无法使用(array)$ object-> record将其转换为数组。我得到这个错误:

Warning: It is not yet possible to assign complex types to properties

+2

永远不要相信'的var_dump()`或。 – 2010-12-01 02:06:55

回答

5

SimpleXML节点是可以包含其他SimpleXML节点的对象。使用iterator_to_array().

+0

我接受了这个答案,因为它允许我完成我的任务。谢谢大家的意见。 – doremi 2010-12-01 17:01:34

4

这不是一个数组。 var_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); 
?> 

输出:

object(SimpleXMLElement)#1 (1) { 
    ["bar"]=> 
    array(2) { 
    [0]=> 
    string(1) "a" 
    [1]=> 
    string(1) "b" 
    } 
} 

object(SimpleXMLElement)#2 (1) { 
    [0]=> 
    string(1) "a" 
} 

你可以通过第二var_dump看到,它实际上是一个SimpleXMLElement

3

我解决了使用count()功能的问题:用SimpleXML`的print_r()`

if(count($xml) > 1) { 
    // $xml is an array... 
} 
相关问题