0

I have these data formats of objects in php.

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [currency] => JPY
            [rate] => 136.07
        )

)

How can I get currency and rate from this object? Thanks in advance..

5
  • Seems like an SimpleXMLElment, are you sure it is a stdClass ? Commented Aug 4, 2015 at 16:12
  • Yeah I have converted Xml data in to object.. And can't figure out further more.. Commented Aug 4, 2015 at 16:17
  • Use the keywords which you have. Google: PHP access XML attributes :) Commented Aug 4, 2015 at 16:19
  • I don't understand why people down vote my question? Though this is unique problem for beginners.. Commented Aug 4, 2015 at 16:24
  • And for downvoters, How can you get japanese currency rate from ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml . Commented Aug 4, 2015 at 16:30

3 Answers 3

2

If that object is named $obj, it's $currencyValue=$obj->{'@attributes'}->currency;

Sign up to request clarification or add additional context in comments.

2 Comments

Err. OP has two stdClass objects inside of each other
Someone has downvoted your answer but it worked for me.. Thanks dude.. I have upvoted it..
0

if that so you can just call

$object->attributes()->currency;

Comments

0

Try these.. ..presuming that you are using SimpleXML to parse the data.

$currency = $object->attributes()->currency;
$rate     = $object->attributes()->rate;

4 Comments

Thanks for answer but it not seems to work.. @RightClick 's answer seems to work fine..
If you have declared the object as a SimpleXML Object, then this will work fine.
@AnilChaudhari Don't convert your xml object into a stdClass object.
@MaggsWeb Yeah I have used SimpleXML .. Here is my snipped $fileContents= file_get_contents($url); $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents); $fileContents = trim(str_replace('"', "'", $fileContents)); $simpleXml = simplexml_load_string($fileContents); $json = json_encode($simpleXml);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.