Home >Backend Development >PHP Problem >How to parse xml to array in php

How to parse xml to array in php

hzc
hzcOriginal
2020-06-05 14:38:053045browse

How to parse xml to array in phpphp

php parses xml and converts it into an array

php parses xml data and converts it into an array. Use simplexml_load_string() to convert the xml object, json_encode() to convert the object to json, and json_decode() to convert it to an array.

  • simplexml_load_file() Load xml url

  • simplexml_load_string() Load xml string

<?php
$xml = &#39;<?xml version="1.0" encoding="utf-8"?>
<res>
    <name>test</name>
    <age>10</age>
    <sex>man</sex>
</res>&#39;;
$xml =simplexml_load_string($xml); //xml转object
$xml= json_encode($xml);  //objecct转json
$xml=json_decode($xml,true); //json转array
 
var_dump($xml);

How to parse xml to array in php

Recommended tutorial: "php tutorial"

The above is the detailed content of How to parse xml to array in php. 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