Home  >  Article  >  Backend Development  >  PHP method and example code for converting XML into array

PHP method and example code for converting XML into array

墨辰丷
墨辰丷Original
2018-06-04 10:21:311224browse

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:

 

 Forty What?
 Joe
 Jane
 
 I know that's the answer -- but what's the question?
 

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn