In web development, XML is widely used as a data transmission and storage format. When we need to use XML format data, we usually need to convert the XML string into an array or object to facilitate subsequent operations. In PHP, you can easily convert XML strings into arrays. This article will introduce how PHP converts XML into an array.
1. Introduction to XML format
XML is a markup language, mainly used for data transmission and storage. XML contains elements and attributes. Element is usually represented by a tag, such as:
<book> <title>PHP从入门到精通</title> <price>50</price> </book>
This XML text defines a book element, which contains title and price sub-elements.
Attribute is a way to add additional information to an element, such as:
<book category="Programming"> <title>PHP从入门到精通</title> <price>50</price> </book>
In this example, we set the category attribute for the book element with the value "Programming".
2. Convert XML to array in PHP
PHP provides many functions for processing XML, among which the more commonly used functions are simplexml_load_string() and xml_parse_into_struct(). Below we introduce the usage of these two functions respectively.
The simplexml_load_string() function can create a SimpleXMLElement object from an XML string, through which elements and attributes in XML can be easily accessed. , you can also convert SimpleXMLElement objects to arrays or other types of data. The following is an example of using the simplexml_load_string() function to convert XML to an array:
$xml = '<bookstore> <book category="Programming"> <title>PHP从入门到精通</title> <author>张三</author> <price>50</price> </book> <book category="Web Development"> <title>JavaScript高级编程</title> <author>李四</author> <price>45</price> </book> </bookstore>'; $data = json_decode(json_encode(simplexml_load_string($xml)), true); print_r($data);
Run the above code, the output is as follows:
Array ( [book] => Array ( [0] => Array ( [@attributes] => Array ( [category] => Programming ) [title] => PHP从入门到精通 [author] => 张三 [price] => 50 ) [1] => Array ( [@attributes] => Array ( [category] => Web Development ) [title] => JavaScript高级编程 [author] => 李四 [price] => 45 ) ) )
As you can see, the simplexml_load_string() function converts XML to A relational array consisting of multiple arrays.
xml_parse_into_struct () function parses an XML string into an array. The following is an example of using the xml_parse_into_struct() function to convert XML into an array:
$xml = '<bookstore> <book category="Programming"> <title>PHP从入门到精通</title> <author>张三</author> <price>50</price> </book> <book category="Web Development"> <title>JavaScript高级编程</title> <author>李四</author> <price>45</price> </book> </bookstore>'; $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $vals); xml_parser_free($p); print_r($vals);
Run the above code, the output is as follows:
Array ( [0] => Array ( [tag] => BOOKSTORE [type] => open [level] => 1 ) [1] => Array ( [tag] => BOOK [type] => open [level] => 2 [attributes] => Array ( [CATEGORY] => Programming ) ) [2] => Array ( [tag] => TITLE [type] => open [level] => 3 ) [3] => Array ( [tag] => TITLE [type] => close [level] => 3 [value] => PHP从入门到精通 ) [4] => Array ( [tag] => AUTHOR [type] => open [level] => 3 ) [5] => Array ( [tag] => AUTHOR [type] => close [level] => 3 [value] => 张三 ) [6] => Array ( [tag] => PRICE [type] => open [level] => 3 ) [7] => Array ( [tag] => PRICE [type] => close [level] => 3 [value] => 50 ) [8] => Array ( [tag] => BOOK [type] => close [level] => 2 ) [9] => Array ( [tag] => BOOK [type] => open [level] => 2 [attributes] => Array ( [CATEGORY] => Web Development ) ) [10] => Array ( [tag] => TITLE [type] => open [level] => 3 ) [11] => Array ( [tag] => TITLE [type] => close [level] => 3 [value] => JavaScript高级编程 ) [12] => Array ( [tag] => AUTHOR [type] => open [level] => 3 ) [13] => Array ( [tag] => AUTHOR [type] => close [level] => 3 [value] => 李四 ) [14] => Array ( [tag] => PRICE [type] => open [level] => 3 ) [15] => Array ( [tag] => PRICE [type] => close [level] => 3 [value] => 45 ) [16] => Array ( [tag] => BOOK [type] => close [level] => 2 ) [17] => Array ( [tag] => BOOKSTORE [type] => close [level] => 1 ) )
As you can see, the xml_parse_into_struct() function parses XML into A relational array consisting of multiple arrays, each of which represents an element or attribute in XML.
3. Use DOMDocument extension for conversion
There is also a method in PHP to use DOMDocument extension to parse XML. DOMDocument is a built-in extension of PHP and is very powerful. The following is an example of using DOMDocument to convert XML data:
$xml = '<bookstore> <book category="Programming"> <title>PHP从入门到精通</title> <author>张三</author> <price>50</price> </book> <book category="Web Development"> <title>JavaScript高级编程</title> <author>李四</author> <price>45</price> </book> </bookstore>'; $dom = new DOMDocument; $dom->loadXML($xml); $data = array(); foreach ($dom->getElementsByTagName('book') as $book) { $data[] = array( 'category' => $book->getAttribute('category'), 'title' => $book->getElementsByTagName('title')->item(0)->textContent, 'author' => $book->getElementsByTagName('author')->item(0)->textContent, 'price' => $book->getElementsByTagName('price')->item(0)->textContent, ); } print_r($data);
Run the above code, the output result is as follows:
Array ( [0] => Array ( [category] => Programming [title] => PHP从入门到精通 [author] => 张三 [price] => 50 ) [1] => Array ( [category] => Web Development [title] => JavaScript高级编程 [author] => 李四 [price] => 45 ) )
As you can see, using DOMDocument to parse XML data, we can easily obtain the XML data elements and attributes, and then converted to an array.
4. Summary
The above are several ways for PHP to convert XML into an array, namely the simplexml_load_string() function, the xml_parse_into_struct() function and the use of DOMDocument extension. Each method has its applicable scenarios. Choose the appropriate method according to actual needs. Of course, no matter which method is used, we need to pay attention to the format and specifications of XML to ensure the correctness and integrity of the data.
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!