-1

I have a variable $foo with this content:

object(SimpleXMLElement)#969 (2) {
  ["@attributes"]=>
  array(1) {
    ["type"]=>
    string(4) "html"
  }
  [0]=>
  string(13) "Viernes Santo"
}

I'm trying to get the content "Viernes Santo" but I can't..I tried $foo[0] but that returns the same content..any idea?

tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • just `echo $foo` and look at the examples in the PHP manual please – Gordon Feb 16 '12 at 18:12
  • possible duplicate of [CRUD the nodes of an XML file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Feb 16 '12 at 18:13

2 Answers2

0

This works:

(string) $foo 
animuson
  • 53,861
  • 28
  • 137
  • 147
tirenweb
  • 30,963
  • 73
  • 183
  • 303
0

The SimpleXMLElement behaves like an object which is why people get confused like you did, but it's actually a language construct which is a little different. The elements and their data must be typecast to the data type you want to work with because by default everything returned from a SimpleXMLElement function is a SimpleXMLElement which if not typecast by a function, must be directly typecast.

If you call:

echo $foo; //typecast to a string automatically
$myVar = $foo; //SimpleXMLElement
$myVar = (string)$foo; //typecast to a string
Brian
  • 3,013
  • 19
  • 27