If you use curl to get the XML data
xml=simplexmlloadstring (data);
Data[′tk′]=jsondecode (Jsonencode (XML), TRUE);
If you get the URL data directly,
Xml=simplexmlloadfile (data);
Data[′tk′]=jsondecode (Jsonencode (XML), TRUE);
Convert the SimpleXML object to JSON first, and then convert the JSON to an array.
Code:
<?php$string = <<<xml<?xml version= ' 1.0 '?> <document> <title>forty what?</title> <from>Joe</from> <to>Jane</to> <body> I know that's the answer-what's the question? </body></document>XML; $xml =simplexml_load_string ($string); $data = Json_decode (Json_encode ($xml), TRUE) var_dump ($xml); Var_dump ($data);
Object (SimpleXMLElement) [1] public ' title ' = = String ' Forty what? ' (length=11) Public ' from ' + = String ' Joe ' (length=3) public ' to ' + = String ' Jane ' (length=4) public ' body ' = str ing ' I know that ' s the answer--and what's the ' s the question? ' (length=57) array ' title ' = = String ' Forty what? ' (length=11) ' From ' = = String ' Joe ' (length=3) ' to ' + ' + string ' Jane ' (length=4) ' body ' = = String ' I know that ' s the answer-- But what ' s the question? ' (length=57)
The above is the whole content of this article, I hope that everyone's study has helped.