How to convert xml to array in php

PHPz
Release: 2023-04-26 09:30:21
Original
412 people have browsed it

In web development, we often need to transmit and store some data in the form of xml, and converting xml data into arrays is also a very common requirement in actual business. As a commonly used web programming language, PHP also provides many tools and functions to process xml data. This article will explain how to convert xml data to an array in PHP.

  1. Using SimpleXML

PHP provides a parser called SimpleXML that can convert xml data into PHP objects. You can convert xml data into an array in PHP by casting the object to an array.

$xml = <<

< book>

PHP Programming John Smith 15.00
Copy after login
Copy after login
Copy after login


Web Development Jane Doe 20.00
Copy after login
Copy after login
Copy after login



XML;

$data = simplexml_load_string($xml);
$array = json_decode(json_encode($data), TRUE);

print_r($array);
?>

The above code first uses XML heredoc (<<

  1. Using DOM

Another way to parse xml into an array is to use PHP's DOM extension. DOM provides some APIs to traverse XML files. We will use the following code:

$xml = <<

PHP Programming John Smith 15.00
Copy after login
Copy after login
Copy after login


Web Development Jane Doe 20.00
Copy after login
Copy after login
Copy after login



XML;

$doc = new DOMDocument();
$doc->loadXML($xml);

$books = $ doc->getElementsByTagName("book");
$result = array();

foreach ($books as $book) {
$item = array(

"name" => $book->getElementsByTagName("name")->item(0)->nodeValue, "author" => $book->getElementsByTagName("author")->item(0)->nodeValue, "price" => $book->getElementsByTagName("price")->item(0)->nodeValue
Copy after login

);
array_push($result, $item);
}

print_r($result);
?>

We first use the loadXML() method Create a DOMDocument object, then obtain all book nodes through the getElementsByTagName() method, traverse each book node, and store the values of its name, author, and price nodes into the PHP array $result.

  1. Using XMLReader

When processing large xml files, the memory consumption of the DOM may be too high, so we can adopt the XMLReader parser, which can be used without a large number of Read the xml document without memory and convert it into a PHP array.

$xml = <<

< book>

PHP Programming John Smith 15.00
Copy after login
Copy after login
Copy after login


Web Development Jane Doe 20.00
Copy after login
Copy after login
Copy after login



XML;

$data = array();
$reader = new XMLReader();
$reader->xml($xml);

while ($reader->read() ) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'book') {

$book = array(); while ($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT) { $name = $reader->name; $reader->read(); $book[$name] = $reader->value; } elseif ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'book') { array_push($data, $book); break; } }
Copy after login

}
}

print_r($data);
?>

We use the xml() method of the XMLReader object to read the xml data into it, and use a while loop to iteratively read each book node and The value of the child node is stored in the PHP array $data.

The above are the specific codes and steps for converting xml data into PHP arrays using three methods. When we need to process xml data, we can choose to use different parser tools to adapt to different needs and performance requirements.

The above is the detailed content of How to convert xml to 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
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!