How to convert XML data to array in php (two methods)

PHPz
Release: 2023-04-14 17:40:52
Original
1740 people have browsed it

PHP is a programming language widely used for server-side programming, while XML is an extensible markup language used by many applications to store and transmit data. In PHP, we can easily convert XML into an array. Using this technology, we can easily process and manipulate XML data.

PHP provides a variety of methods to convert XML into arrays. Below we will introduce two commonly used methods.

Method 1: Use simple XML extension

Simple XML extension is an extension that comes with PHP, which can help us quickly convert XML data into PHP arrays. We only need to call the simplexml_load_string() function, which will return a SimpleXMLElement object based on the XML string. We can obtain XML data through the properties and methods of the object.

The following is a simple example:

$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J.K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>';

$xml = simplexml_load_string($xmlString);

print_r($xml);
Copy after login

The output result is as follows:

SimpleXMLElement Object
(
    [book] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [category] => COOKING
                        )

                    [title] => Everyday Italian
                    [author] => Giada De Laurentiis
                    [year] => 2005
                    [price] => 30.00
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [category] => CHILDREN
                        )

                    [title] => Harry Potter
                    [author] => J.K. Rowling
                    [year] => 2005
                    [price] => 29.99
                )
        )
)
Copy after login

We can see that the SimpleXMLElement object is converted into an array, and each node becomes An element of an array, its attributes also become a key-value pair.

Method 2: Use PHP DOM extension

PHP DOM extension is a widely used PHP extension that can be used to process and manipulate XML and HTML documents. We can use the methods it provides to convert XML files into DOM objects, and then convert them into arrays through DOM related methods.

The following is a simple example:

$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J.K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>';

$dom = new DOMDocument();
$dom->loadXML($xmlString);
$books = $dom->getElementsByTagName('book');
$data = array();

foreach ($books as $book) {
    $bookData = array(
        'category' => $book->getAttribute('category'),
        'title' => $book->getElementsByTagName('title')->item(0)->nodeValue,
        'author' => $book->getElementsByTagName('author')->item(0)->nodeValue,
        'year' => $book->getElementsByTagName('year')->item(0)->nodeValue,
        'price' => $book->getElementsByTagName('price')->item(0)->nodeValue
    );

    $data[] = $bookData;
}

print_r($data);
Copy after login

The output is as follows:

Array
(
    [0] => Array
        (
            [category] => COOKING
            [title] => Everyday Italian
            [author] => Giada De Laurentiis
            [year] => 2005
            [price] => 30.00
        )

    [1] => Array
        (
            [category] => CHILDREN
            [title] => Harry Potter
            [author] => J.K. Rowling
            [year] => 2005
            [price] => 29.99
        )
)
Copy after login

We can see that using the PHP DOM extension, we can convert the XML file into a DOM Object, and use the methods provided by the DOM object to obtain the value of each node, and finally convert it into a PHP array.

Summary

Whether you use a simple XML extension or a PHP DOM extension, you can easily convert XML files into PHP arrays. We just need to choose the appropriate method according to our needs. Using PHP arrays, we can easily process and manipulate XML data.

The above is the detailed content of How to convert XML data to array in php (two methods). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!