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

How to convert xml into array in php

PHPz
Release: 2023-04-20 13:36:09
Original
552 people have browsed it

PHP, as a scripting language, has the characteristics of object-oriented programming and functional programming. PHP is widely used in the field of web development. In terms of XML data processing, PHP also provides many useful functions and methods. This article will introduce how to use PHP to convert an XML string into an array.

First, we need to install the PHP DOM extension. DOM extension is an extension that processes XML documents in an object-oriented manner. We can use its DOMDocument class to process XML. For the specific installation process, please refer to the official PHP documentation and will not be repeated here.

Next, we need to convert the XML string into a DOM object. There are two methods provided here: one is to load the XML string into the DOM object through the loadXML() method, and the other is to load the XML file into the DOM object through the load() method. Here we demonstrate loading an XML string through the loadXML() method:

$xml_string = '<root><person><name>John</name><age>30</age></person><person><name>Mary</name><age>28</age></person></root>';
$dom = new DOMDocument();
$dom->loadXML($xml_string);
Copy after login

In this way, we convert the XML string into a DOM object. Next, we need to convert the DOM object into an array. The DOM object has some useful functions and methods for processing XML documents, the most commonly used of which are the getElementsByTagName() and getAttribute() methods. Because the data structure of XML is a tree structure, these methods can help us traverse the XML document.

We can convert the DOM object into an array through a recursive function:

function dom2array($node) {
    $array = array();
    if($node->hasAttributes()) {
        foreach($node->attributes as $attr) {
            $array[$attr->nodeName] = $attr->nodeValue;
        }
    }
    if($node->hasChildNodes()) {
        foreach($node->childNodes as $child) {
            if($child->nodeType == XML_TEXT_NODE) {
                $array['_value'] = $child->nodeValue;
                continue;
            }

            $name = $child->nodeName;
            if(!isset($array[$name])) {
                $array[$name] = dom2array($child);
            } else {
                if(!is_array($array[$name])) {
                    $array[$name] = array($array[$name]);
                }
                $array[$name][] = dom2array($child);
            }
        }
    }
    return $array;
}

$xml_array = dom2array($dom->documentElement);
print_r($xml_array);
Copy after login

This function will recursively traverse all nodes, using the node name as the key of the array. If there is a node with the same name, then Stored in array mode. Through the above code, we get the following output:

Array
(
    [person] => Array
        (
            [0] => Array
                (
                    [name] => John
                    [age] => 30
                )
            [1] => Array
                (
                    [name] => Mary
                    [age] => 28
                )
        )
)
Copy after login

As you can see, we successfully converted the XML string into an array. In this way, we can process XML data conveniently, and we can also use PHP to process XML data more efficiently during development.

The above is the detailed content of How to convert xml into array in php. For more information, please follow other related articles on the PHP Chinese website!

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