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.
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
Web Development Jane Doe 20.00
XML;
$data = simplexml_load_string($xml);
$array = json_decode(json_encode($data), TRUE);
print_r($array);
?>
The above code first uses XML heredoc (<<
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
Web Development Jane Doe 20.00
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
);
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.
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
Web Development Jane Doe 20.00
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; } }
}
}
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!