This time for you to bring the array and XML files to each other, the array and XML files to convert each other considerations are what, the following is the actual case, take a look.
Recently paid, the server returned is an XML file, so you need to convert an array, it will be easy to operate, well, not much to say, directly on the code:
1. XML-to-array
/** * Convert XML to array * @param string $xml XML string or XML file name * @param bool $isfile whether the XML file name was passed in @return array conversion. Array */function xmltoarray ($xml, $isfile =false) { //disallow referencing of external XML entities Libxml_disable_entity_loader (true); if ($isfile) { if (!file_exists ($xml)) return false; $xmlstr = file_get_contents ($xml); } else{ $xmlstr = $xml; } $result = Json_decode (Json_encode (simplexml_load_string ($xmlstr, ' simplexmlelement ', Libxml_nocdata)), true); return $result;}
Usage examples:
$xmlDoc =<<<eto<books> <book> <author>jack herrington</author> <title>php Hacks</title> <publisher>o ' reilly</publisher> </book> <book> <author>jack herrington</author> <title>podcasting hacks</title> <publisher>o ' Reilly</publisher> </book> <book> <author>xml formatting </author> <title> scripting Home online tools </title> <publisher >tools.jb51.net</publisher> </book></books>ETO; $relarr =xmltoarray ($XMLDOC);p Rint_r ($relarr );
Operation Result:
Array ([book ] = = Array ( [0] = = Array ( [author] + Jack Herrington [title] = PHP Hacks [Publisher] = O ' Reilly ) [1] = = Array ( [author] + Jack Herrington [title] = Podcasting Hacks [Publisher] + O ' Reilly ) [2] = = Array ( [author] + XML format C17/>[title] + Script Home online tool [Publisher] = tools.jb51.net) ) )
2. Array to XML
/** * Array to XML character * @param string $xml XML string **/function arraytoxml ($data) { if (!is_array ($data) | | count ($DATA) <= 0) { return false; } $xml = "<xml>"; foreach ($data as $key = + $val) { if (is_numeric ($val)) { $xml. = "<". $key. " > ". $val." </". $key." > "; } else{ $xml. = "<". $key. " ><! [cdata[]. $val. "] ></". $key." > "; } } $xml. = "</xml>"; return $xml;}
Usage examples:
$arrDoc = Array ("Author" = "XML format", "title" and "Script home online Tool", "publisher" and "Tools.jb51.net"); $xmlrel = Arraytoxml ($ARRDOC);//Run Result: <xml><author><! [Cdata[xml formatted]]></author><title><! [cdata[Script's home online tool]]></title><publisher><! [cdata[tools.jb51.net]]></publisher></xml>
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Vue Making picture Carousel
JS gets the value within the first element in the Select drop-down box
Installation electron failure how to deal with