If you use curl to get the XML data
xml=simplexmlloadstring (data);
Data[′tk′]=jsondecode (Jsonencode (XML), TRUE);
If you're getting URL data directly,
xml=simplexmlloadfile (data);
Data[′tk′]=jsondecode (Jsonencode (XML), TRUE);
Convert the SimpleXML object to JSON, 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--but what ' s 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 ' out ' => string ' Joe ' (length=3) public
' to ' => string ' Jane ' (length=4) public
' body ' => string '
I know that ' the answer--but what ' 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--the question of but what ' s?
(length=57)
The above is the entire content of this article, I hope to help you learn.