This article mainly introduces in detail the method of converting XML into an array in PHP. Interested friends can refer to it
If you use the xml data obtained by curl
xml=simplexmlloadstring(data);
data[′tk′]=jsondecode(jsonencode(xml),TRUE);
If the URL data is obtained directly
xml=simplexmlloadfile(data);
data[′tk′]=jsondecode(jsonencode(xml),TRUE);
Convert the simplexml object to json first, and then json Convert to 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 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' => string ' I know that's 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 -- but what's the question? ' (length=57)
Summary: The above is this article The entire content, I hope it will be helpful to everyone's study.
Related recommendations:
How PHP implements the data paging display function
The subclass in PHP overrides the parent class Method (parent:: method name)
PHP method to operate MongoDB and simple analysis
The above is the detailed content of PHP method and example code for converting XML into array. For more information, please follow other related articles on the PHP Chinese website!