PHP: json_decode(json_encode($xml));

php

Solution

If `$xml` is a `SimpleXml` object, you can't access some of its attributes directly. It's a trick that is used to convert `SimpleXml` object to a classical object and get access to all of its attributes :)

Also, you can pass a boolean parameter to get an array instead of an object: `json_decode(json_encode($xml), true);`

Problem

I was reading some code and saw the following line ``` $obj = json_decode(json_encode($xml)); ``` `$xml` is from `simplexml_load_string` so to me it looks like that the line is equivalent of ``` $obj = $xml; ``` What might be reason for the seemingly unnecessary encoding and decoding?

Original source