If you receive such a string in your program:
| The code is as follows |
Copy Code |
| <ReportList><ordIndex>1</ordIndex><ordLabNo>1942268</ordLabNo><arcItemId> 134</arcitemid><orditemdesc> Product 1</orditemdesc><status> Execution </Status><ordDate> The 2013-08-12</orddate><reportstatus> report has been </reportstatus><reportexception>0</ Reportexception></reportlist><reportlist><ordindex>2</ordindex><ordlabno> 19434368</ordlabno><arcitemid>135</arcitemid><orditemdesc> Products 2</orditemdesc>< Status> Executive </Status><ordDate>2013-05-12</ordDate><reportStatus> report not out </reportstatus ><reportException>0</reportException></ReportList> |
Well, congratulations, some of the methods we use in PHP
Will not take effect, such as:
| The code is as follows |
Copy Code |
|
$array = (array) new SimpleXMLElement ($xml _str);
$array = (array) simplexml_load_string ($xml _str);
$array = Json_decode (Json_encode (simplexml_load_string ($xml _str)), true); All returns False |
So we'll just have to write a way ourselves.
The code is as follows:
| The code is as follows |
Copy Code |
function Parse_xml_to_array ($XMLSTR, $loopTag) {
$args = Explode (' </'. $loopTag. > ', $XMLSTR);
$returns = Array ();
if ($args) {
$reg = '/< (\w+) [^>]*> ([\x00-\xff]*) <\/\1>/';
foreach ($args as $item) {
$item = Str_replace (' < '. $loopTag. > ', ', $item);
if (Preg_match_all ($reg, $item, $matches)) {
if (Isset ($matches [1]) && isset ($matches [2])) {
$returns [] = Array_combine ($matches [1], $matches [2]);
}
}
}
}
Unset ($args);
return $returns;
}
$arr = Parse_xml_to_array ($xml, ' reportlist ');
Var_dump ($arr);
|
Continue to read about the article