errata
errata

Reputation: 6031

Getting information from an XML object in PHP

I am using some XML parser to get some information from API, blah blah... :)

In one place in my script, I need to convert string to int but I'm not sure how...

Here is my object:

object(parserXMLElement)#45 (4) {
  ["name:private"]=>
  string(7) "balance"
  ["data:private"]=>
  object(SimpleXMLElement)#46 (1) {
    [0]=>
    string(12) "11426.46"
  }
  ["children:private"]=>
  NULL
  ["rows:private"]=>
  NULL
}

I need to have this string "11426.46" stored in some var as integer.
When I echo $parsed->result->balance I get that string, but if I want to cast it as int, the result is: 1.

Please help!
Thanks a lot!

Upvotes: 0

Views: 147

Answers (2)

ChrisF
ChrisF

Reputation: 137108

You need to use intval. For example:

echo intval($parsed->result->balance);

will output the value as an integer - assuming that balance is a string.

Upvotes: 0

Sergey Eremin
Sergey Eremin

Reputation: 11080

you have an object, intval of an object will always be 1(if it doesnt have a __toString() magic method defined). you can intval SimpleXMLElement and it will return 11426, but to do that, the data member of the parserXMLElement class has to be public. you might need to define a getData() method for the parserXMLElement class or make the data member public.

Upvotes: 2

Related Questions