As shown below:
Copy Code code as follows:
?
/**
* Xml2array () would convert the given XML text to a array in the XML structure.
* link:http://www.bin-co.com/php/scripts/xml2array/
* Arguments: $contents-the XML text
* $get _attributes-1 or 0. If This is 1 the function would get the attributes as as the the tag values-this results in a different array structure In the return value.
* $priority-can be ' tag ' or ' attribute '. This'll change the way the resulting array sturcture. For ' tag ', the tags are given more importance.
* Return:the parsed XML in an array form. Use Print_r () to the resulting array structure.
* Examples: $array = Xml2array (file_get_contents (' feed.xml '));
* $array = Xml2array (file_get_contents (' Feed.xml ', 1, ' attribute '));
*/
function Xml2array ($contents, $get _attributes=1, $priority = ' tag ') {
if (! $contents) return array ();
if (!function_exists (' xml_parser_create ')) {
Print "' xml_parser_create () ' function not found! ';
return Array ();
}
Get the XML parser of php-php must have this module for the parser to work
$parser = Xml_parser_create (");
Xml_parser_set_option ($parser, xml_option_target_encoding, "UTF-8"); # Http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
Xml_parser_set_option ($parser, xml_option_case_folding, 0);
Xml_parser_set_option ($parser, Xml_option_skip_white, 1);
Xml_parse_into_struct ($parser, Trim ($contents), $xml _values);
Xml_parser_free ($parser);
if (! $xml _values) return;//hmm ...
Initializations
$xml _array = Array ();
$parents = Array ();
$opened _tags = Array ();
$arr = Array ();
$current = & $xml _array; Refference
Go through the tags.
$repeated _tag_index = Array ();//multiple tags with same name'll be turned to an array
foreach ($xml _values as $data) {
Unset ($attributes, $value);//remove existing values, or there'll be trouble
This command would extract these variables into the foreach scope
Tag (string), type (string), level (int), attributes (array).
Extract ($data);//we could use the array by itself, but this cooler.
$result = Array ();
$attributes _data = Array ();
if (Isset ($value)) {
if ($priority = = ' Tag ') $result = $value;
else $result [' value '] = $value; Put the "value in" a assoc array if we are in the ' Attribute ' mode
}
Set the attributes too.
if (Isset ($attributes) and $get _attributes) {
foreach ($attributes as $attr => $val) {
if ($priority = = ' tag ') $attributes _data[$attr] = $val;
else $result [' attr '] [$attr] = $val; Set all the attributes in a array called ' attr '
}
}
The tag status and do the needed.
if ($type = = "Open") {//the starting of the tag ' <tag> '
$parent [$level-1] = & $current;
if (!is_array ($current) or (!in_array ($tag, Array_keys ($current))) {//insert New tag
$current [$tag] = $result;
if ($attributes _data) $current [$tag. ' _attr '] = $attributes _data;
$repeated _tag_index[$tag. ' _ '. $level] = 1;
$current = & $current [$tag];
else {//there is another element with the same tag name
if (Isset ($current [$tag][0])) {//if There is a 0th element it's already an array
$current [$tag] [$repeated _tag_index[$tag. ' _ '. $level]] = $result;
$repeated _tag_index[$tag. ' _ '. $level]++;
else {//this section'll make the value of an array if multiple tags with the same name appear together
$current [$tag] = Array ($current [$tag], $result);//this'll combine the existing item and the new item together to Array
$repeated _tag_index[$tag. ' _ '. $level] = 2;
if (Isset $current [$tag.] _attr ']) {//the of the last (0th) tag must is moved as OK
$current [$tag] [' 0_attr '] = $current [$tag.] _attr '];
Unset ($current [$tag.] _attr ']);
}
}
$last _item_index = $repeated _tag_index[$tag. ' _ '. $level]-1;
$current = & $current [$tag] [$last _item_index];
}
} elseif ($type = = "complete") {//tags, ends in 1 line ' <tag/> '
If the key is already taken.
if (!isset ($current [$tag])) {//new Key
$current [$tag] = $result;
$repeated _tag_index[$tag. ' _ '. $level] = 1;
if ($priority = = ' Tag ' and $attributes _data) $current [$tag. ' _attr '] = $attributes _data;
else {//if taken, put all things inside a list (array)
if (Isset ($current [$tag][0]) and Is_array ($current [$tag])) {//if It is already an array ...
... push the new element into the. Array.
$current [$tag] [$repeated _tag_index[$tag. ' _ '. $level]] = $result;
if ($priority = = ' Tag ' and $get _attributes and $attributes _data) {
$current [$tag] [$repeated _tag_index[$tag. ' _ '. $level]. ' _attr '] = $attributes _data;
}
$repeated _tag_index[$tag. ' _ '. $level]++;
} else {//if it is isn't an array ...
$current [$tag] = Array ($current [$tag], $result); //... Make it an array using using the existing value and the new value
$repeated _tag_index[$tag. ' _ '. $level] = 1;
if ($priority = = ' Tag ' and $get _attributes) {
if (Isset $current [$tag.] _attr ']) {//the of the last (0th) tag must is moved as OK
$current [$tag] [' 0_attr '] = $current [$tag.] _attr '];
Unset ($current [$tag.] _attr ']);
}
if ($attributes _data) {
$current [$tag] [$repeated _tag_index[$tag. ' _ '. $level]. ' _attr '] = $attributes _data;
}
}
$repeated _tag_index[$tag. ' _ '. $level]++; 0 and 1 index is already taken
}
}
} elseif ($type = = ' Close ') {//end of tag ' </tag> '
$current = & $parent [$level-1];
}
}
Return ($xml _array);
}
?>
Function Description and Example:
Copy Code code as follows:
$arr = Xml2array (file_get_contents ("Tools.xml"), 1, ' attribute ');