本文章來給各位同學介紹兩個簡單的實例,php 數組轉xml與xml轉換數組,希望此文章對各位朋友會有所幫助。
php 數組轉xml,代碼如下:
- function array2xml($array, $xml = false){
- if($xml === false){
- $xml = new SimpleXMLElement('<root/>');
- }
- foreach($array as $key => $value){
- if(is_array($value)){
- array2xml($value, $xml->addChild($key));
- }else{
- $xml->addChild($key, $value);
- }
- }
- return $xml->asXML();
- }
- header('Content-type: text/xml');
- print array2xml($array);
php xml轉數組,代碼如下:
- $s = <<<EOS
- <root>
- <Formula>
- <formulaname>Basic</formulaname>
- <movespeed>1</movespeed>
- <box>4</box>
- <chicken>3</chicken>
- <ducks>1</ducks>
- <cereal>2</cereal>
- </Formula>
- </root>
- EOS;
- $a = json_decode(json_encode((array) simplexml_load_string($s)),1);
- print_r($a);
新聞熱點
疑難解答