tonyrocks
tonyrocks

Reputation: 609

How do I print Attribute Values of an object(stdClass) in PHP

I'm having a difficult time understanding how to print out an attribute value of an object. The particular example I am working from is this:

    object(SimpleXMLElement)#1 (1) {
  ["links"]=>
  object(SimpleXMLElement)#4 (2) {
    ["@attributes"]=>
    array(3) {
      ["total-matched"]=>
      string(2) "31"
      ["records-returned"]=>
      string(2) "10"
      ["page-number"]=>
      string(1) "3"
    }

I want to print the value of the links total-matched (which is 31). I've tried this: echo $object->links->total-matched; but I only get the value of 0.

How can I do this?

Upvotes: 0

Views: 81

Answers (1)

lupatus
lupatus

Reputation: 4248

$object->links->total-matched evaluates as $object->link->total - matched (- is minus, I suppose you should see warning about using unknown constant - turn on error reporting). To access attributes with names like this you can do following: $object->links->{'total-matched'} although in this case, since it's SimpleXML attribute, I think you need to get attributes array:

$attr = $object->links->attributes();
echo $attr['total-matched'];

Upvotes: 1

Related Questions