array(2) { [0]=> object(SimpleXMLEle...">

Why is_array () returns false?

I have this SimpleXML object:

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

Why is_array ($ object-> record) returns false? Obviously, this is an array. Why can't I detect it with is_array?

Also, I cannot use it as an array using (array) $ object-> record. I get this error:

A warning. It is not yet possible to assign complex types to properties

+6
arrays oop php simplexml
source share
3 answers

SimpleXML nodes are objects that can contain other SimpleXML nodes. Use iterator_to_array().

+5
source share

This is not an array. The var_dump output is misleading. Consider:

 <?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); ?> 

Output:

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

As you can see from the second var_dump , this is actually a SimpleXMLElement .

+4
source share

I solved the problem using the count() function:

 if( count( $xml ) > 1 ) { // $xml is an array... } 
+3
source share

All Articles