Home > Article > Backend Development > How to convert xml to array in php
When developing web applications, it is often necessary to obtain XML data from external sources and process it. Many times, we need to convert these XML data into PHP arrays to make it easier to add, modify, delete or access the data.
In PHP, we can use the SimpleXML function library to parse and manipulate XML files, and we can also use the DOM function library to accomplish the same task. However, this article will focus on how to use the SimpleXML library to convert an XML file into a PHP array.
Before converting the XML file to a PHP array, we need to load the XML file first. This can be done through the constructor of the SimpleXMLElement class as follows:
$xml = new SimpleXMLElement('filename.xml', null, true);
Here, the first parameter is the path to the XML file, the second parameter specifies the options for the XML file, and the third parameter indicates whether Enable namespace support.
Once we have loaded the XML file, we can convert it to a PHP array using the methods provided by the SimpleXML library. First, we need to create an empty array to hold the XML data. Then, we can use the children() method of the SimpleXMLElement class to get all the child nodes of the current node, as shown below:
$array = array(); foreach ($xml->children() as $element) { $name = $element->getName(); $attributes = $element->attributes(); if (!$attributes) { if (!isset($array[$name])) { $array[$name] = (string) $element; } else { if (!is_array($array[$name])) { $array[$name] = array($array[$name]); } $array[$name][] = (string) $element; } } else { $array[$name][] = array(); foreach ($attributes as $attrName => $attrValue) { $array[$name][count($array[$name])-1]['_'.$attrName] = (string) $attrValue; } if ($element->children()) { $array[$name][count($array[$name])-1] = array_merge($array[$name][count($array[$name])-1],$this->xml2array($element)); } else { $array[$name][count($array[$name])-1]['value'] = (string) $element; } } }
Here, we use a foreach loop to traverse all the child nodes and get the name and value of each child node. Attributes. For nodes without child nodes, we will save their content as the value of the array, and its name is the node name; for nodes with child nodes, the xml2array() function is called recursively to convert its child nodes into arrays, and then These arrays are merged into the current node's array.
After converting the XML data into a PHP array, the output array may need further processing to better meet our needs. For example, we might need to store all properties in separate arrays, or change the array structure to better match the needs of the application. Here are some common tidying operations you may want to do:
function xml2array($xml) { $array = array(); foreach ($xml->children() as $element) { $name = $element->getName(); $attributes = $element->attributes(); if (!$attributes) { if (!isset($array[$name])) { $array[$name] = (string) $element; } else { if (!is_array($array[$name])) { $array[$name] = array($array[$name]); } $array[$name][] = (string) $element; } } else { $array[$name][] = array('_attributes' => array()); foreach ($attributes as $attrName => $attrValue) { $array[$name][count($array[$name])-1]['_attributes'][$attrName] = (string) $attrValue; } if ($element->children()) { $array[$name][count($array[$name])-1] = array_merge($array[$name][count($array[$name])-1],$this->xml2array($element)); } else { $array[$name][count($array[$name])-1]['value'] = (string) $element; } } } return $array; }
function xml2array($xml) { $array = array(); foreach ($xml->children() as $element) { $name = $element->getName(); $attributes = $element->attributes(); if (!$attributes) { if (!isset($array[$name])) { $array[$name] = (string) $element; } else { if (!is_array($array[$name])) { $array[$name] = array($array[$name]); } $array[$name][] = (string) $element; } } else { $node = array( '_name' => $name, '_value' => (count($element->children()) == 0) ? (string) $element : null, '_attributes' => array(), '_children' => array() ); foreach ($attributes as $attrName => $attrValue) { $node['_attributes'][$attrName] = (string) $attrValue; } foreach ($element->children() as $childElement) { $childNode = $this->xml2array($childElement); $childName = $childElement->getName(); if (count($childNode) == 1) { $node['_children'][$childName] = reset($childNode); } else { $node['_children'][$childName][] = $childNode; } } if (!isset($array[$name])) { $array[$name] = $node; } else { if (!is_array($array[$name])) { $array[$name] = array($array[$name]); } $array[$name][] = $node; } } } return $array; }
Here, when processing a node, we create a new array containing the following four elements: name, value, attribute, and child node. This allows the output array to be easily mapped into a database table, class attribute, or other data structure.
Summary
Converting XML files to PHP arrays in PHP can be achieved through the SimpleXML function library. You can use the children() method of the SimpleXMLElement class to traverse the XML tree and save the data using a PHP array. Once converted to a PHP array, the data can be further processed for easy access and manipulation.
The above is the content introduced in this article. I hope it can be helpful to you in using PHP to process XML files in web development.
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!