How to parse xml to array in php

hzc
Release: 2023-03-01 12:18:01
Original
2933 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!