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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With